簡體   English   中英

如何在 asp.net 核心 3.1(或更高版本)MVC 中的 controller 后操作中綁定復雜 model 中的數據?

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

如何在 asp.net 核心 3.1(或更高版本)MVC 中的 controller 后操作中綁定復雜 model 中的數據?

這只是一個示例 model,但要點沒有展平,也沒有單獨的視圖模型。

給定一個示例 model ,如下所示:

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;}
}

所有單個屬性都顯示在視圖中。

您如何將它專門綁定到 controller 上的 post 方法?

你如何綁定復雜的屬性?

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

}

假設業務 class 是您的視圖 model,試試這個

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(Business model) 
{
      ...your code
}

如果要綁定“Id”、“Name”、“WebAddress”等屬性,可以直接使用[Bind("Id,Name, WebAddress")] ,但是如果要綁定一些特定的嵌套Model的屬性,可以在 class 中添加[Bind("")]

例如,如果我想綁定NameStreet屬性,我可以使用以下代碼:

controller

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

地址 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; }
    }

看法

<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>

當我提交表單時,我只是綁定StreetName屬性

在此處輸入圖像描述

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM