0

我有一个名为 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>

创建肌动蛋白方法是:-

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

1 回答 1

2

乍一看,我想这是因为您传递给视图的模型与您要求返回的模型不同。更改此行:

public ActionResult Create(Server server, TechnologyIP technologyIP)

public ActionResult Create(ServerToEdit serverToEdit)

这行:

repository.InsertOrUpdateServer(server,technologyIP);

repository.InsertOrUpdateServer(serverToEdit.Server, serverToEdit.technologyIP);

让我们知道您的身体情况如何。

于 2013-07-25T20:42:54.493 回答