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>

Create actin メソッドは次のとおりです。

[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 に答える