我有以下用于创建新网络信息的操作方法:-
public ActionResult CreateVMNetwork(int vmid)
{
VMAssignIps vmips = new VMAssignIps()
{
TechnologyIP = new TechnologyIP() { TechnologyID = vmid},
IsTMSIPUnique = true,
IsTMSMACUnique = true
};
return PartialView("_CreateVMNetwork",vmips);
}
[HttpPost]
public ActionResult CreateVMNetwork(VMAssignIps vmip)
{
if (ModelState.IsValid)
{
try
{
repository.InsertOrUpdateVMIPs(vmip.TechnologyIP,User.Identity.Name);
repository.Save();
return PartialView("_networkrow",vmip);
}
catch (Exception ex)
{
ModelState.AddModelError(string.Empty, "Error occurred: " + ex.InnerException.Message);
}
}
return PartialView("_CreateVMNetwork", vmip);
}
我有以下 _CreateVMNetwork 视图:-
@model TMS.ViewModels.VMAssignIps
@using (Ajax.BeginForm("CreateVMNetwork", "VirtualMachine", new AjaxOptions
{
InsertionMode = InsertionMode.InsertAfter,
UpdateTargetId = "networktable",
LoadingElementId = "loadingimag",
HttpMethod= "POST"
}))
{
@Html.ValidationSummary(true)
@Html.HiddenFor(model=>model.TechnologyIP.TechnologyID)
@Html.Partial("_CreateOrEditVMNetwork", Model)
<input type="submit" value="Save" class="btn btn-primary"/>
}
和 _CreateOrEditVMNetwork 视图:-
@model TMS.ViewModels.VMAssignIps
<div>
<span class="f">IP Address</span>
@Html.EditorFor(model => model.TechnologyIP.IPAddress)
@Html.ValidationMessageFor(model => model.TechnologyIP.IPAddress)
<input type="CheckBox" name="IsTMSIPUnique" value="true" @(Html.Raw(Model.IsTMSMACUnique ? "checked=\"checked\"" : "")) /> |
<span class="f"> MAC Address</span>
@Html.EditorFor(model => model.TechnologyIP.MACAddress)
@Html.ValidationMessageFor(model => model.TechnologyIP.MACAddress)
<input type="CheckBox" name="IsTMSMACUnique" value="true" @(Html.Raw(Model.IsTMSMACUnique ? "checked=\"checked\"" : "")) />
</div>
我面临的问题是,如果添加新实体时出现模型状态错误,则会显示部分视图,模型状态错误如下:-
所以我的问题是,如果有办法用局部视图显示模型状态错误,而不像我目前所做的那样更新表格行“插入之后”?谢谢