Search code examples
asp.net-coredata-bindingasp.net-core-mvc

How do you bind data in a complex model in a controller post action in asp.net core 3.1 (or above) MVC?


How do you bind data in a complex model in a controller post action in asp.net core 3.1 (or above) MVC ?

This is just an example model, but the takeaways are no flattening and no seperate viewmodel.

Given an example model like this:

public class Business
{
   public int Id {get; set;}
   public string Name {get; set;}
   public string WebAddress {get; set;}
   public ContactDetails ContactInfo {get; set;}
   public Address AddressInfo {get; set;}
}

public class ContactDetails
{
   public string TelNo {get; set;}
   public string Mobile {get; set;}
}

public class Address 
{
   public string BuildingNo {get; set;}
   public string Street {get; set;}
   public string City {get; set;}
   public string Postcode {get; set;}
}

All the individual properties appear in the in the view.

How would you specifically bind this to the post method on a controller?

How do you bind the complex properties?

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create([Bind("Name,WebAddress")] DeliCakeBiz deliCakeBiz) <--- ???
{

}

Solution

  • If you want to bind properties like "Id" , "Name" , "WebAddress",you can use [Bind("Id ,Name , WebAddress")] diretly , But If you want to bind Some specific nested Model's properties, you can add [Bind("")] in the class.

    For example , If I want to bind Name and Street properties , I can use this code:

    controller

            [HttpPost]
            public async Task<ActionResult> ComplexModel([Bind("AddressInfo,Name")] Business bus)
            {
                //.......
                return View();        
            }
    

    Address model

        [Bind("Street")]
        public class Address
        {
            public string BuildingNo { get; set; }
            public string Street { get; set; }
            public string City { get; set; }
            public string Postcode { get; set; }
        }
    

    view

    <form asp-action="ComplexModel">
        <div class="form-group">
            <label asp-for="Id"></label>
            <input type="text" asp-for="Id" />
        </div>
        <div class="form-group">
            <label asp-for="Name "></label>
            <input type="text" asp-for="Name " />
        </div>
        <div class="form-group">
            <label asp-for="WebAddress "></label>
            <input type="text" asp-for="WebAddress " />
        </div>
        <div class="form-group">
            <label asp-for="ContactInfo.TelNo"></label>
            <input type="text" asp-for="ContactInfo.TelNo" />
        </div>
        <div class="form-group">
            <label asp-for="ContactInfo.Mobile "></label>
            <input type="text" asp-for="ContactInfo.Mobile " />
        </div>
        <div class="form-group">
            <label asp-for="AddressInfo.BuildingNo"></label>
            <input type="text" asp-for="AddressInfo.BuildingNo" />
        </div>
        <div class="form-group">
            <label asp-for="AddressInfo.Street"></label>
            <input type="text" asp-for="AddressInfo.Street" />
        </div>
        <div class="form-group">
            <label asp-for="AddressInfo.City"></label>
            <input type="text" asp-for="AddressInfo.City " />
        </div>
        <div class="form-group">
            <label asp-for="AddressInfo.Postcode"></label>
            <input type="text" asp-for="AddressInfo.Postcode" />
        </div>
        <button type="submit">submit</button>
    </form>
    

    When I submit the form , I just bind Street and Name properties

    enter image description here

    enter image description here