简体   繁体   中英

How to pass model as a parameter to [HttpPost] ActionMethod in ASP.NET MVC?

I have a usual grid which has edit and save button. When I click the Edit link from the grid it goes to Edit action method, where I can read the Id as a parameter. I am thinking since I can pass the Id then why not the whole model as a parameter to the Edit action? However, I am always getting null . I looked at some examples they all shows passing the id rather than the model object. I wonder why model cannot be passed? Even though when I debug the code I always see item row is binding with the parameter Student from item.

View for Grid with Edit link:

@model IEnumerable<MVC_BasicTutorials.Models.Student>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.StudentName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Age)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.StudentName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Age)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.StudentId, std=item }) |
     
        </td>
    </tr>
}

</table>

Controller action method:

// Get
public ActionResult Edit(int Id, Student std)
{
    var checkSet=std; // coming null
    var checkId=Id;  //i am seeing the id value

    return RedirectToAction("Index");
}

Try the following:

public ActionResult Index()
{
    var model = /* prepare view data model */;
    return View(model);
}

[HttpPost]
// The second parameter `Id` is removed because of the `Student` already contains it.
public ActionResult Edit(Student std)
{
    System.Diagnostics.Debug.WriteLine($"Id= {std.StudentId}, Student Name= {std.StudentName} ");

    return RedirectToAction("Index");
}

And in the Index.cshtml :

@model IEnumerable<MVC_BasicTutorials.Models.Student>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.StudentName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Age)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        using (Html.BeginForm("Edit", "Home"))
        {
            @* Prepare a hidden data context to the able the MVC data binding work correctly *@
            @Html.Hidden("StudentId", item.StudentId)
            @Html.Hidden("StudentName", item.StudentName)
            @Html.Hidden("Age", item.Age)

            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.StudentName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Age)
                </td>
                <td>
                    @*@Html.ActionLink("Edit", "Edit", new { id = item.StudentId, std = item }) *@
                    <input type="submit" value="Edit" />
                </td>
            </tr>
        }
    }
</table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM