【问题标题】:Binding a Nested Object in ASP.NET MVC - Razor在 ASP.NET MVC 中绑定嵌套对象 - Razor
【发布时间】:2021-09-24 01:39:28
【问题描述】:

我的 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 页面中正确绑定地址对象,使其在表单提交后可用。

在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 中正确绑定上述视图模型,以及在表单提交时如何在 Action 方法中正确检索属性。

【问题讨论】:

  • 您是否尝试过使用 Helper Model 表示 Customer 和 Address 的模型组合?
  • @ashhadullah..我不熟悉..你能解释一下
  • 您好,请问还有什么可以帮助您的吗?

标签: c# .net asp.net-mvc asp.net-core model-binding


【解决方案1】:

这是一个小例子

    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

【讨论】:

    【解决方案2】:

    你可以试试这个方法。

    客户端:

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

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

    输出:

    希望对您有所帮助。如果您有任何进一步的疑虑,请告诉我。

    【讨论】:

    • 所以在 ViewModel 端不需要更改??地址对象上的绑定是如何发生的 ..所以如果我将隐藏字段设置为 @Html.Hidden("AddressId", Model.Address.AddressId )..那么提交时是否可用??
    • 无需更改您的模型。你可以保持原样,我只是做了这个例子来保留你所有的代码。
    • 只需复制粘贴我的示例,它就可以正常工作,我在这里不用担心,如果您遇到任何问题,请告诉我。
    • 如果我设置了@Html.Hidden("AddressId", Model.CustomerAddress.AddressId)..那么它在action方法中是否可以作为model.CustomerAddress.AddressId ??
    • 不,它会抛出异常。因为加载时的值会是空的,这就是原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 2016-09-27
    • 2016-10-17
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多