Question

I have a webgrid with an actionlink which I want to use to edit the selected row in a new view. I know that I can use an actionlink to supply an arguement to my actionresult method but I don't know how to pass the webgrid's row data to my controller apart from using actionlink.

@{
    ViewBag.Title = "Index";
}
<h2>
    Index</h2>
<p>
    @Html.ActionLink("Create New User", "CreateUser")
</p>
<div class="webgrid-wrapper">
    @model IEnumerable<UserManager.Models.vw_UserManager_Model_Add_Users>
@{
    ViewBag.Title = "Jobs";
    WebGrid grid = new WebGrid(Model, canPage: true, canSort: true, rowsPerPage: 15, selectionFieldName: "selectedRow", fieldNamePrefix: "gridItem");

}
    @grid.GetHtml(
    fillEmptyRows: true,
        tableStyle: "webgrid",
                alternatingRowStyle: "webgrid-alternating-row",
                headerStyle: "webgrid-header",
                footerStyle: "webgrid-footer",
                selectedRowStyle: "webgrid-selected-row",
            rowStyle: "webgrid-row-style",
        mode: WebGridPagerModes.All,
columns: new[] {
    //grid.Column("ApplicationId"),
    //grid.Column("salutation"),

    //grid.Column("PasswordSalt"),

     grid.Column("FirstName"),

    //grid.Column("LoweredEmail"),
    //grid.Column("PasswordQuestion"),
    //grid.Column("PasswordAnswer"),

    // grid.Column("PasswordFormat"),
    grid.Column("LastName"),
    grid.Column("Password"),
    grid.Column("isactive"),
    //grid.Column("IsLockedOut"),
    grid.Column("email"),
   grid.Column("module_name"), 

    //grid.Column("LastLoginDate"),
    //grid.Column("LastPasswordChangedDate"),
    //grid.Column("LastLockoutDate"),

    //grid.Column("FailedPasswordAttemptCount"),
    //grid.Column("FailedPasswordAttemptWindowStart"),
    //grid.Column("FailedPasswordAnswerAttemptCount"),
    //grid.Column("FailedPasswordAnswerAttemptWindowStart"),
   // Rest of grid columns, seen previously
    @*grid.Column(
       "",
        header: "Actions",
        format: @<text>
    @Html.ActionLink("Edit", "Edit", new { id = item.email }) 
    </text>
    )   , 
     grid.Column(
       "",
        header: "Actions",
        format: @<text>
    @Html.ActionLink("Delete", "Delete", new { id = item.email })
    </text>
    )    *@
@*     grid.Column(
            header:"", 
            format:@<text><div id="btnSelectedRow">
                "@item.GetSelectLink("Edit")</div></text>
            ),*@


     grid.Column(
            header:"", 
            format:@<text><div id="btnSelectedRow">
                "@Html.ActionLink("Edit record", 
                        "EditUser",
                        "UserManager",
                                       new {selectedRow = grid.SelectedRow },
                        null
                        )</div></text>
            )
})
    @if (grid.HasSelection)
    {
        var record = grid.SelectedRow;
@*@RenderPage("~/Views/UserManager/EditUser.cshtml", new { record = grid.SelectedRow })*@

    }
</div>
<script type="text/javascript">
    $(document).ready(function () {
        function jQueryUIStyling() {
            $('input:button, input:submit').button();

            // Style tables.
            $('.webgrid-wrapper').addClass('ui-grid ui-widget ui-widget-content ui-corner-all');
            $('.webgrid-title').addClass('ui-grid-header ui-widget-header ui-corner-top');
            jQueryTableStyling();
        }

        //
        // Style tables using jQuery UI theme. This function is 
        // split out separately so that it can be part of the AJAX
        // callback of the WebGrid WebHelper in ASP.NET MVC.
        //
        function jQueryTableStyling() {
            $('.webgrid').addClass('ui-grid-content ui-widget-content');
            $('.webgrid-header').addClass('ui-state-default');
            $('.webgrid-footer').addClass('ui-grid-footer ui-widget-header ui-corner-bottom ui-helper-clearfix');
        }
    });

</script>

My controller

 public ActionResult EditUser(System.Web.Helpers.WebGrid record)
        {
            record.ToString();

            return View();
        }

Summary

How to pass row data using ActionLink from one view to another via ActionResult method.

Was it helpful?

Solution

You have two options, really: have your action take the user Id and look the user up, or have each row of your grid contain a form which sends all the user details to the action when you click 'edit'. Which of those you choose depends on what your action is supposed to do:

  1. If it redirects to a form where the user can edit the user they selected, use the first approach
  2. If it takes all the user details and saves an updated user, use the second approach

Edit

To pass the id you can use this for the action link:

@Html.ActionLink(
    "Edit record",
    "EditUser",
    "UserManager",
    new { id = item.email })

...and this for the action signature:

public ActionResult EditUser(string id)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top