微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决在 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??
    }

任何人都可以分享有关如何使用剃刀模板在 cshtml 中正确绑定上述视图模型以及如何在表单提交时在 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>
}

表单输出应该是这样的:

enter image description here

控制器:

        [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()`;
        }

注意:请尝试按照 Model property 描述在表单上传递值。除了你可能 获得 null 值。

输出:

enter image description here

希望能帮到你。如果您有任何进一步的疑虑,请告诉我。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。