我有以下主菜单,其中包含一个 Ajax.Actinolink 来呈现部分视图,如下所示:-
@model TMS.ViewModels.VMJoin
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div class="row-fluid sortable">
//code goes here
<div class="box-content">
<p>
@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>
//code goes here
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
操作方法是:-
[HttpPost]
[ValidateAntiForgeryToken]
[CheckUserPermissions(Action = "Edit", Model = "Virtual Machine")]
public ActionResult CreateVMNetwork(AssignIps vmip)
{
//code goes here
if ((Request.Form["IsTMSIPUnique"] == "true") && (!IT360ipunique || !TMSipunique))
{
return Json(new { IsSuccess = "False", description = "IP Address Already Exsists." }, JsonRequestBehavior.AllowGet);
}
if ((Request.Form["IsTMSMACUnique"] == "true") && (!IT360macunique || !TMSmacunique))
{
return Json(new { IsSuccess = "False", description = "MAC Address Already Exsists." }, JsonRequestBehavior.AllowGet);
}
if (String.IsNullOrEmpty(vmip.TechnologyIP.IPAddress) || String.IsNullOrEmpty(vmip.TechnologyIP.MACAddress))
{
return Json(new { IsSuccess = "False", description = "Please Make sure to insert both values." }, JsonRequestBehavior.AllowGet);
}
if (ModelState.IsValid)
{
try
{
string ADusername = User.Identity.Name.Substring(User.Identity.Name.IndexOf("\\") + 1);
repository.InsertOrUpdateIPs(vmip.TechnologyIP,ADusername);
repository.Save();
return PartialView("_networkrow",vmip);
}
catch (Exception ex)
{
//ModelState.AddModelError(string.Empty, "Error occurred: " + ex.InnerException.Message);
return Json(new { IsSuccess = "False", description = ex.InnerException.InnerException.Message.ToString() }, JsonRequestBehavior.AllowGet);
}
}
return PartialView("_CreateVMNetwork", vmip);
}
来自动作方法调用的渲染部分视图是:-
@model TMS.ViewModels.AssignIps
@if (this.ViewContext.FormContext == null)
{
this.ViewContext.FormContext = new FormContext();
}
@using (Ajax.BeginForm("CreateVMNetwork", "VirtualMachine", new AjaxOptions
{
InsertionMode = InsertionMode.InsertAfter,
UpdateTargetId = "networktable",
LoadingElementId = "loadingimag",
HttpMethod= "POST",
OnSuccess="submitform"
}))
{
@Html.ValidationSummary(true)
@Html.HiddenFor(model=>model.TechnologyIP.TechnologyID)
@Html.AntiForgeryToken()
<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\"" : "")) /> IP Unique. |
<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\"" : "")) /> MAC Unique.
</div>
<input type="submit" value="Save" class="btn btn-primary"/>
}
@Scripts.Render("~/bundles/jqueryval")
但我面临的问题是,当点击渲染部分视图内的保存按钮时,将发送两个发布请求。我认为问题与我有两个对@Scripts.Render("~/bundles/jqueryval")
脚本的引用有关;一个在主视图中,另一个在局部视图中。但同时我不能删除任何这些引用,因为在主视图上我需要 ajax 功能,而在部分视图上我需要 jquery 验证。
任何人都可以建议如何解决这个重复的请求问题吗?