我的 ASP .NET MVC 中有以下创建操作方法: -
public ActionResult CreateVMNetwork(int vmid)
{
VMAssignIps vmips = new VMAssignIps()
{
TechnologyIP = new TechnologyIP() { TechnologyID = vmid},
IsTMSIPUnique = true,
IsTMSMACUnique = true
};
return PartialView("_CreateVMNetwork",vmips);
}
将呈现以下部分视图: -
@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)
<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>
<input type="submit" value="Save" class="btn btn-primary"/>
}
在以下主视图中,单击Ajax.actionlink后: -
@Ajax.ActionLink("Add Network Info", "CreateVMNetwork","VirtualMachine",
new { vmid = Model.VirtualMachine.TMSVirtualMachineID },
new AjaxOptions {
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "AssignNetwork" ,
LoadingElementId = "progress"
}
)
</p>
<p><img src="~/Content/Ajax-loader-bar.gif" class="loadingimage" id="progress" /></p>
<div id ="AssignNetwork"></div>
然后点击“Save
”按钮时,它将调用以下操作方法: -
[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);
}
何时会呈现以下部分视图_networkrow
: -
@model TMS.ViewModels.VMAssignIps
<tr id="@Model.TechnologyIP.ID">
<td> @Model.TechnologyIP.IPAddress</td>
<td>@Model.TechnologyIP.MACAddress</td>
<td>@Ajax.ActionLink("Delete",
"DeleteNetworkInfo", "VirtualMachine",
new { id = Model.TechnologyIP.ID },
new AjaxOptions
{ Confirm = "Are You sure You want to delete (" + Model.TechnologyIP.IPAddress + ")" + "( " + Model.TechnologyIP.MACAddress + ").",
HttpMethod = "Post",
OnSuccess = "deletionconfirmation",
OnFailure = "deletionerror"
})</td>
</tr>
除非发生模型状态错误或异常,否则上述所有操作都将正常工作,然后在这种情况下,将使用部分视图更新表,并且模型状态将显示在带有字段的表下。但我需要在同一原始视图上显示模型状态错误。因此,只有在没有发生异常或模型状态错误时才需要Ajax.begin表单来更新表,并在原始局部视图中显示错误消息,而不是在表格下面。
有人可以就如何解决这个问题提出建议吗?
答案 0 :(得分:1)
我没有清楚地提出您的问题,但我认为您应该在视图中放置一个<div>
来显示错误。
然后,如果您在处理过程中遇到一些错误,请通过ViewBag
将一些错误消息推送到模型。
因此,您的操作方法将如下所示:
[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)
{
ViewBag.ErrorMessage = "Some messages...";
// also any other info you like
}
}
return PartialView("_CreateVMNetwork", vmip);
}
在你看来像这样:
<div>
<p>@ViewBag.ErrorMessage</p>
</div>
所以,如果你有一些错误,他们就会出现在
中答案 1 :(得分:0)
网络表在哪里定义?请记住,即使存在异常(因为已经捕获它)或ModelState错误,ajax响应将返回状态200并且将执行OnSuccess。在保存按钮的情况下,在任何网络表的html已经包含之后,将插入来自服务器的返回响应。如果您希望在发生故障时采取特殊操作,则必须更改服务器上的响应状态,并通过ajax表单上的OnFailure调用的客户端上的函数捕获它。