4

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.

  1. The 'selectedRow' value does come into the controller.
  2. The 'UID_CUSTOMER' value always comes in as NULL.
  3. 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

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
John D
  • 517
  • 7
  • 22
  • may be this solution adding @Html.ActionLink in column on webgrid can help you http://stackoverflow.com/questions/6167903/using-data-in-a-html-actionlink-inside-a-webgrid-column-not-possible – Daniel Gpe Reyes Jul 02 '15 at 19:18
  • Thank you, Daniel, for your reply. I tried every option in the link and cannot get the UID_CUSTOMER to pass into the controller's action parameters. Thanks for your assistance, but I did not find a solution. Thanks anyway. – John D Jul 03 '15 at 00:54

0 Answers0