在 ASP.NET MVC 中绑定嵌套对象 - Razor

Binding a Nested Object in ASP.NET MVC - Razor

我的ASP.NET MVC视图模型设计如下

public class Customer
{
  public int CustomerId{ get; set; }
  public string CustomerName{ get; set; }
  public Address CustomerAddress {get;set;}
}


 public class Address
{
   public int AddressId{ get; set; }
   public string HouseName{ get; set; }
   public string Location{get;set;}
}

如何在 cshtml 页面中正确绑定 Address 对象,以便它在表单提交后可用。

在 cshtml 页面中,我想将其绑定如下属性

 @model CustomerManagement.Model.ViewModels.Customers.Customer
 @using (Html.BeginForm())
   {
     @Html.EditorFor(model => model.CustomerName, new { htmlAttributes = new { @class = "form- 
   control readOnlySchdCode", @readonly = "readonly" } })
    @Html.Hidden("AddressId", Model.Address.AddressId)
    @Html.Hidden("HouseName", Model.Address.HouseName)
   }

在控制器表单中提交将如下所示

    public async Task<ActionResult> AddCustomer(Customer model)
    {
       //How can i access Address properties eg:model.CustomerAddress.AddressId??
    }

谁能分享一个示例代码,说明如何使用 razor 模板在 cshtml 中正确绑定 Above viewmodel,以及如何在提交表单时在 Action 方法中正确检索属性。

这是一个小例子

    public class BigViewModel : IModelOptions
    {
       public bool Confirm { get; set; }
       public SmallViewModel SmallView { get; set; }
    }

    public class SmallViewModel
    {
       public string Stuff{ get; set; }
    }

    public interface IModelOptions
    {
       SmallViewModel SmallView { get; set; }
    }

我们的控制器就像

    public class HomeController : Controller
    {
       public ActionResult Index()
       {
          return View();
        }

        [HttpPost]
        public ActionResult Index(BigViewModel model)
        {
           var smallVIewModelInfo = model.SmallView.Stuff;
           var bigViewModelConfirm = model.Confirm;
           return View();
        }
    }

使用像

这样的视图
    @model MvcApplication1.Models.BigViewModel

你可以这样试试

客户端:

@using Newtonsoft.Json.Linq
@using WebAppDemo.Models
@model WebAppDemo.Models.Customer

@{
    ViewData["Title"] = "Home Page";
}


@{
    ViewBag.Title = "Home Page";
}
<br />
@using (Html.BeginForm("AddCustomer", "Home", FormMethod.Post, new { id = "Form1" }))
{
    <div class="row">
        <div class="col-lg-2">Cust Id</div>
        <div class="col-lg-10">
            @Html.TextBoxFor(a => a.CustomerId, new { @class = "form-control" })
        </div>
    </div>
    <br />
    <div class="row">
        <div class="col-lg-2">Customer Name</div>
        <div class="col-lg-10">
            @Html.TextBoxFor(a => a.CustomerName, new { @class = "form-control" })
        </div>
    </div>
    <br />
    <div class="row">
        <div class="col-lg-2">Address</div>
        <div class="col-lg-10">
            @Html.TextBoxFor(a => a.CustomerAddress.HouseName, new { @class = "form-control" }) 
        </div>
    </div>
    <br />
    <div class="row">
        <div class="col-lg-12"><input type="submit" value="Submit" class="btn btn-primary"></div>

    </div>
}

表单输出应该是这样的:

控制器:

        [HttpPost] //attribute to get posted values from HTML Form
        public  ActionResult AddCustomer(Customer model)
        {
            return Ok();// For testing I just kept it as `Ok` You can return `View()`;
        }

Note: Please try to pass value on form as per Model property descriptions. Other than you might get null value.

输出:

希望对您有所帮助。如果您还有任何疑问,请告诉我。