How to bind model with list of object in mvc 5

lastcoder 1 Reputation point
2022-04-24T02:59:07.84+00:00

How can we pass a complex list with class back to controller?

public class PersontViewModel
    {

        public string Name{ get; set; }


        public AddressViewModel objAddress { get; set; }

        public List<AssetsViewModel> objAssets { get; set; }
    }



//------------------------
@Model PersonVIewModel

@using (Html.BeginForm("CreatePerson", "Person", FormMethod.Post,new { enctype = "multipart/form-data" }))
        {

            <input asp-for="@Model.objAddress.Street" type="hidden" id="hdnN" />            
            <input asp-for="@Model.objAddress.City" type="hidden" id="hdnd" />


                for(int i=0;i < Model.objAssets.Count; i++)
                {

                    <input asp-for="objAssets[i].Name" type="hidden" id="hdnat" />

                        @Html.Hidden("Exam",@Model.objsset[i].AssetName ) //this posts null

                        @Model.objAsett[i].AssetID</td>
                        @Model.objAsett[i].AssetName</td>//This presents on the view fine.
                 }
Developer technologies ASP.NET Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Sreeju Nair 12,666 Reputation points
    2022-04-24T04:47:59.227+00:00

    Based on the query you posted, I understand the following.

    • Your data binding part is working fine, except for the hidden field. If this is the case try the following for creating the hidden field.

    @azzedinehtmlsql .HiddenFor(m => m.objsset[i].AssetName)

    The @azzedinehtmlsql .Hidden() creates a hidden field with the name you specify. In the Model binding context, it is better to use Html.HiddenFor, where it create the hidden input field that will be binded to the correct property.

    Hope this helps

    0 comments No comments

  2. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2022-04-25T05:56:42.48+00:00

    Hi @lastcoder ,

    public List<AssetsViewModel> objAssets { get; set; }  
    <input asp-for="objAssets[i].Name" type="hidden" id="hdnat" />  
                               
                             @Html.Hidden("Exam",@Model.objsset[i].AssetName ) //this posts null  
          
                             @Model.objAsett[i].AssetID</td>  
                             @Model.objAsett[i].AssetName</td>//This presents on the view fine.  
    

    Is this the code you copied and pasted, I found that @Model.objAssets is misspelled, suggest you check the spelling of all objAssets.

    The Html.Hidden creates a hidden input but you have to specify the name and all the attributes you want to give that field and value. The Html.HiddenFor creates a hidden input for the object that you pass to it, they look like this:

    @Html.Hidden("yourProperty",model.yourProperty);  
      
    @Html.HiddenFor(m => m.yourProperty)  
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.