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

后端 未结 3 2062
名媛妹妹
名媛妹妹 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:29

    Your objects could be so big! Query string's has a limitation on how much data you can pass via those based on the browser. You should consider passing a unique id value (of the record) and using which get the entire record from db in your action method and pass that to the view.

    @foreach(var item in SomeCollection)
    {
      
         @Html.Action("Update me!", "Update", new {  id = item.Id }) 
      
    }
    

    and in the action method

    public ActionResult Update(int id)
    {
        var item = GetItemFromId(id);
        return View(item);
    }
    

    Assuming GetItemFromId method returns the method/view model from the unique id value. Basically you get the entire record using this unique id from your db table/repository.

提交回复
热议问题