Pass entire object from view to controller in ASP.NET MVC 5

后端 未结 3 2063
名媛妹妹
名媛妹妹 2021-01-20 02:27

Is there a way to pass entire object from ASP.NET MVC 5 View to a Controller? This is my situation:

  • I have a View that displays all rows from a DB table
3条回答
  •  悲哀的现实
    2021-01-20 03:30

    Assuming that your Update View isn't of type IEnumerable...

    You just need to pass the ID of the record that you want to send to the Update view...

    Like so:

    @Html.Action("Update me!", "Update", new { id = item.ID })
    

    Then your Update action would look like this:

    [HttpGet]
    public ActionResult Update(int id)
    {
        var parameter = db/* connection string variable */.TableName.Find(id);
        return View(parameter);
    }
    

    Then your link should work appropriately.

    Hope this helps!

提交回复
热议问题