Reputation: 16239
I have created a web application in mvc3 and created two partial views one having controls like dropdownlist. second having webgrid which shows data from database.
partialview1.cshtml
@model Mapping.Models.SecurityIdentifierMapping
@using (Html.BeginForm("Mapping", "Home"))
{
@Html.DropDownList("SecurityID", Model.PricingSecurityID, "-- Select SecurityID --")
<br />
@Html.DropDownList("CUSIPID", Model.PricingSecurityID, "-- Select CUSIPID --")
<br />
<button type="submit">Map</button>
}
partialview2.cshtml
@model IEnumerable<Mapping.Models.SecurityIdentifierMapping>
@{
ViewBag.Title = "Mapping";
WebGrid grid = null;
if (Model.Count() > 0 ){
grid = new WebGrid(source: Model,
defaultSort: "Id",
canPage: true,
canSort: true,
rowsPerPage:20);
}
}
<h2>Mapping</h2>
@if (grid != null)
{
@grid.GetHtml(
tableStyle: "grid",
headerStyle: "head",
alternatingRowStyle: "alt",
columns: grid.Columns(
grid.Column("", header: null, format: @<text>@Html.ActionLink("Edit", "Edit", new { id = (int)item.id }) @Html.ActionLink("Delete", "Delete", new { id = (int)item.id })</text>),
grid.Column("PricingSecurityID"),
grid.Column("CUSIP")
)
)
}
<br />
<p>
@Html.ActionLink("Back", "Index")
</p>
in index.cshtml
<div>
@Html.Partial("_ControlsPartial",)
</div>
<div>
@Html.Partial("_WebGridPartial")
</div>
HomeController.cs
public ActionResult Index()
{
SecurityIdentifierMapping objModel = new SecurityIdentifierMapping();
objModel.PricingSecurityID = objRepository.GetPricingSecurityID();
objModel.CUSIP = objRepository.GetCUSIP();
return View(objModel);
}
How can i show webgrid and populate dropdown with same Index()??
getting error :( what should be 2nd parameter inside @Html.Partial() so that both grid and control works fine on same page.?
Upvotes: 1
Views: 2998
Reputation: 1039508
You are passing a SecurityIdentifierMapping
model to the Index
view. Inside this Index view you are calling 2 partials:
@Html.Partial("_ControlsPartial")
and:
@Html.Partial("_WebGridPartial")
The first one works fine because it is strongly typed to SecurityIdentifierMapping
but the second one (the one with the grid) doesn't work because it is strongly typed to IEnumerable<SecurityIdentifierMapping>
. Thus the exception you are getting.
I would recommend you using a view model which will contain 2 properties: one simple SecurityIdentifierMapping
that you could pass to the first partial and an IEnumerable<SecurityIdentifierMapping>
property that you will pass to the second partial. It is the controller action that will fill this view model and pass it to the Index view:
Index.cshtml
:
@model MyViewModel
<div>
@Html.Partial("_ControlsPartial", Model.SecurityIdentifier)
</div>
<div>
@Html.Partial("_WebGridPartial", Model.Identifiers)
</div>
Upvotes: 1