0

我有一个名为 Server 的模型类,并且我创建了一个新的 ServerToEdit viewModel 类,但是当我尝试提交 viewModel 时,我会在 repository.save() 方法上收到以下错误。

传入字典的模型项的类型为“TMS.Models.Server”,但该字典需要“TMS.ViewModels.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");
            }

最后 InsertOrUpdateServer 存储库方法是:-

public void InsertOrUpdateServer(Server server, TechnologyIP technologyIP)
        {
            if (server.ServerID == default(int))
            {
                // New entity
                int technologyypeID = GetTechnologyTypeID("Server");
                Technology technology = new Technology
                {
                    IsDeleted = true,
                    TypeID = technologyypeID,
                    Tag = "S" + GetTagMaximumeNumber(technologyypeID).ToString()

                };

                InsertOrUpdateTechnology(technology);
                Save();



                var auditinfo = IntiateAudit(tms.AuditActions.SingleOrDefault(a => a.Name.ToUpper() == "ADD").ActionID,
                    tms.TechnologyTypes.SingleOrDefault(a => a.Name.ToUpper() == "Server").AssetTypeID,
                 "TDMGROUP\administrator", technology.TechnologyID);

                server.ServerID = technology.TechnologyID;
                technologyIP.TechnologyID = technology.TechnologyID;
                tms.Servers.Add(server);
                InsertOrUpdateTechnologyIP(technologyIP);
                technology.IsDeleted = false;
                InsertOrUpdateTechnology(technology);
                InsertOrUpdateAudit(auditinfo);
            }
            else
            {
                // Existing entity
                var auditinfo = IntiateAudit(tms.AuditActions.SingleOrDefault(a => a.Name.ToUpper() == "EDIT").ActionID,
                    tms.TechnologyTypes.SingleOrDefault(a => a.Name.ToUpper() == "Server").AssetTypeID,
                 "TDMGROUP\administrator", server.ServerID);
                tms.Entry(server).State = EntityState.Modified;
                tms.Entry(technologyIP).State = EntityState.Modified;
                InsertOrUpdateAudit(auditinfo);
            }
        }

提前感谢您的帮助?

4

1 回答 1

1

首先,将您的 ViewModel 更改为:

public class ServerToEdit
{
    public Server Server { get; set; }
    public TechnologyIP TechnologyIP { get; set; }
}

并且,将您的操作更改为:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ServerToEdit serverToEdit)
{
   if (ModelState.IsValid)
   {
       try 
       { 
           repository.InsertOrUpdateServer(serverToEdit.Server, serverToEdit.TechnologyIP);
           repository.Save();
           return RedirectToAction("Index");
       }
       catch
       {
           // Some code... 
       }
   }
}

然后,在您的视图中,您将拥有:

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

@Html.HiddenFor(model => model.TechnologyIP.TechnologyID)
<div class="editor-label">
   IP Address
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.TechnologyIP.IPAddress)
    @Html.ValidationMessageFor(model => model.TechnologyIP.IPAddress)
</div>    

<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>
于 2013-07-22T13:53:44.310 回答