Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

ViewComponent Model binding problem

$
0
0

In my case CurrentAddress and PermanentAddress objects is null on Post Action Register of Register Controller.
kindly help me for same below are the code snap. Thanks in advance

below is Model class

public class RegisterModel
    {
        [Display(Name = "First Name")]
        public string FirstName { get; set; }

        [Display(Name = "Last Name")]
        public string LastName { get; set; }

        [Display(Name = "Current Address")]
        public AddressDetails CurrentAddress { get; set; }

        [Display(Name = "Permanent Address")]
        public AddressDetails PermanentAddress { get; set; }

    }
    public class AddressDetails
    {
        [Display(Name = "Address Line 1")]
        public string AddressLine1 { get; set; }

        [Display(Name ="Address Line 2")]
        public string AddressLine2 { get; set; }        
    }

Controller class

public class RegisterController : Controller
    {        
        public IActionResult Index()
        {
            RegisterModel model = new RegisterModel();
            model.CurrentAddress = new AddressDetails();
            model.PermanentAddress = new AddressDetails();
            return View(model);
        }

        [HttpPost]
        public IActionResult Register([FromForm]RegisterModel model)
        {
            if(ModelState.IsValid)
            {

            }
            return View("Index");
        }
    }

ViewComponent class

public class AddressViewComponent : ViewComponent
    {
        public IViewComponentResult Invoke(AddressDetails model)
        {
            return View(model);
        }
    }

ViewComponent.cshtml class

@model WebApplication6.Models.AddressDetails<div class="container"><div class="form-group">
        @Html.LabelFor(m => m.AddressLine1)
        @Html.TextBoxFor(m => m.AddressLine1, new { @class = "form-control" })</div><div class="form-group">
        @Html.LabelFor(m => m.AddressLine2)
        @Html.TextBoxFor(m => m.AddressLine2, new { @class = "form-control" })</div></div>

Register.cshtml class

Register.cshtml class
@model RegisterModel<hr /><form method="post" class="form-horizontal" asp-action="Register" asp-controller="Register"><div class="container"><div class="form-group">
            @Html.LabelFor(m => m.FirstName)
            @Html.TextBoxFor(m => m.FirstName, new { @class = "form-control" })</div><div class="form-group">
            @Html.LabelFor(m => m.LastName)
            @Html.TextBoxFor(m => m.LastName, new { @class = "form-control" })</div><div class="form-group">
            @Html.LabelFor(m => m.CurrentAddress)
            @Component.Invoke("Address", Model.CurrentAddress)</div><div class="form-group">
            @Html.LabelFor(m => m.PermanentAddress)
            @Component.Invoke("Address", Model.PermanentAddress)</div><input type="submit" value="Submit" class="btn btn-default" /></div></form>


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>