![](/img/trans.png)
[英]do you have to qualify control property names when using the default asp.net mvc model binder
[英]Why asp.net model binder can not map a string values to a Object property that have the same name
我有一個名為Server的模型類,我創建了一個新的ServerToEdit viewModel類,如下所示: -
public class ServerToEdit
{
public Server Server { get; set; }
[Required]
public String IPAddress { get; set; }
}
創建視圖的一部分是: -
model TMS.ViewModels.ServerToEdit
@* This partial view defines form fields that will appear when creating and editing entities *@
@Html.AntiForgeryToken()
<div class="editor-label">
@Html.LabelFor(model => model.Server.CustomerName)
</div>
<div class="editor-field">
@Html.EditorFor(model =>model.Server.CustomerName)
@Html.ValidationMessageFor(model =>model.Server.CustomerName)
</div>
<div class="editor-label">
IP Address
</div>
<div class="editor-field">
@Html.EditorFor(model => model.IPAddress)
@Html.ValidationMessageFor(model => model.IPAddress)
</div>
IPAddress
<div class="editor-label">
@Html.LabelFor(model =>model.Server.ILOIP)
</div>
<div class="editor-field">
@Html.EditorFor(model =>model.Server.ILOIP)
@Html.ValidationMessageFor(model =>model.Server.ILOIP)
</div>
Create actin方法是: -
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Server server, TechnologyIP technologyIP)
{
try
{
if (ModelState.IsValid)
{
repository.InsertOrUpdateServer(server,technologyIP);
repository.Save();
return RedirectToAction("Index");
}
但模型綁定器無法將我的視圖中的IPAddress字段綁定到TechnologyIP.IPAddress屬性? 技術IP模型類是: -
public class TechnologyIP
{
public String IPAddress { get; set; }
public int ID { get; set; }
}
初看起來,我會想到這是因為您傳遞給視圖的模型與您要求的模型不同。 改變這一行:
public ActionResult Create(Server server, TechnologyIP technologyIP)
至
public ActionResult Create(ServerToEdit serverToEdit)
這一行:
repository.InsertOrUpdateServer(server,technologyIP);
至
repository.InsertOrUpdateServer(serverToEdit.Server, serverToEdit.technologyIP);
讓我們知道您的身體情況如何。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.