0
votes

I am doing work on form where user can enter a customer record....View is scaffold with Create controller. On 'Create' View, user can enter 'engineNo' to check its details which passes to another action "CheckRecord",,it can be seen from view...

<form>
    <input type="text" id="enginNo" />
    <input type="button" value="search" id="btnSearch" />
</form>

@using (Html.BeginForm("Index","Home",FormMethod.Get)) 
{
    @Html.AntiForgeryToken()
    <div id="info">
@{Html.RenderAction("CheckRecord","Sales");}
</div>
   some create fields
  }

The Create and "CheckRecord" actions are,,

public ActionResult Create()
{
ViewBag.CustomerId = new SelectList(db.CustomersDMs, "CustomerId", "Name");
ViewBag.SMClientBranchId = new SelectList(db.SMClientBranchesDMs, "SMClientId", "Name");
  ViewBag.EngineNumber = new SelectList(db.StockDMs, "EngineNumber", "ChasisNumber");
return View();
}

public ActionResult CheckRecord(string enginNo)
{
var results = db.StockDMs.Where(c=>c.EngineNumber ==enginNo);
return PartialView("_part",results);
}

And my partialview,,,

@model IEnumerable<SM.CRM.AutosLoan.Models.Core.DomainModels.StockDM>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.AutoCompanyBrand.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.SMClientBranch.Name)
        </td>
}

My problem is, the partial view is rendered correctly but the Model of partial view doesn't have value,,,Why is that, i am doing something wrong...Please help,,,Thanks for your time

1
instead of return PartialView("_part",results); try return PartialView("_part",results.ToList()); - Kartikeya Khosla
What do you mean "partial view doesn't have value,"? You have 2 forms, the first does not even have a control which will post back anything so a little hard to understand what you doing here. - user3559349
Thanks for reply, after putting checks on partial view, it seems statement (var item in Model) is terminated after Model - Suhail Mumtaz Awan
That'll happen if the model is empty. You're not passing any "enginNo" to the action you posted. - Mackan
You not passing a value to the parameter enginNo so you query is in effect .Where(c => c.EngineNumber == null); - user3559349

1 Answers

2
votes

(Posting this as answer since I mentioned it in comments and that's not the correct place)

Your action CheckRecord(string enginNo) takes an argument of enginNo, but you're calling it without any argument. That in turn means your db lookup will most likely not return any results, unless you get results on..

var results = db.StockDMs.Where(c => c.EngineNumber == null);

Make sure the action gets a valid argument, for example:

@{ Html.RenderAction("CheckRecord", "Sales", new { enginNo = "abc123" }); }