I have researched and cannot find an answer that I can use to pass the ID value back to the controller when user clicks on the SELECT cell of a WebGrid row using mvc4 asp.net vs2012.
My WebGrid looks like this in the (partial) view:
@{
ViewBag.Title = "Customers";
WebGrid grid = new WebGrid(source: Model.oCustItems, canPage: false, rowsPerPage: 50,
selectionFieldName: "selectedRow",
ajaxUpdateContainerId: "grid", // pointer to the grid-control as defined with this new WebGrid call.
sortFieldName: "Active", defaultSort: "Active");
}
<div id="divForm" class="webgrid_div_container">
@using (Html.BeginForm("GridRowClicked", "MainPage", FormMethod.Post))
{
@Html.Hidden("oValue", 1)
grid.SelectedIndex=ViewBag.SelectedIndex;
@grid.GetHtml(
...styling here... ,
columns: grid.Columns(
grid.Column(null,null, format: @<input type="hidden" name="hidUID_CUSTOMER" value="@item.UID_CUSTOMER"/>),
grid.Column(columnName: "CustomerID", format: (item) => item.GetSelectLink(item.CustomerID.ToString()), header: "Select", style: "webgrid-select"),
grid.Column(columnName: "CompanyName", header: "Company Name"),
grid.Column(columnName: null, header: "Inspection", format: @<text>06/28/2015</text>),
grid.Column(columnName: "Active", header: "Active?", format: @<text><input type="checkbox" checked="@item.Active" disabled="disabled" /></text>)
),
htmlAttributes: new { id = "MainTable" }
)
if (grid.HasSelection)
{
<input type="hidden" id="UID_CUSTOMER" name="UID_CUSTOMER" value="@grid.SelectedRow["UID_CUSTOMER"]"/>
@Html.Hidden("UID_CUSTOMER", grid.SelectedRow["UID_CUSTOMER"])
}
}
</div>
Here is what I have discovered.
- The 'selectedRow' value does come into the controller.
- The 'UID_CUSTOMER' value always comes in as NULL.
- The (grid.hasSelection) fires AFTER the re-render after the return from controller.
What I need is to obtain the 'UID_CUSTOMER' value (located in the first column (hidden) so that it is a valid value when actioned to the controller action. But I cannot seem to find a way to obtain and send the value to the controller declared as:
[HttpGet]
public ActionResult Index(Int32? UID_CUSTOMER, Int32? selectedRow)
{
Any help will be appreciated. I have pre-novice level jQuery and javascript skills, and beginner mvc skills (mainly web-forms .net skills) so please reply accordingly...Thanks...John