0

我有以下主菜单,其中包含一个 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 验证。

任何人都可以建议如何解决这个重复的请求问题吗?

4

1 回答 1

0

您实际上不需要渲染脚本两次。只要在加载部分视图的主视图中呈现脚本,您就可以访问脚本的功能。解决方案不是再次加载脚本,而是使用脚本提供的功能来复制它在加载时所做的事情。使用验证模块再次解析 html 元素。这可以通过对以下函数的简单调用来完成:

$.validator.unobtrusive.parse(selector)

您不需要再次解析整个 html 页面,只需解析新插入的元素。这样,您就可以在主视图中加载所有 ajax 和不显眼的功能,并在加载局部视图时调用它,这样一切都可以在没有双重发布的情况下进行。

于 2013-12-19T14:52:31.160 回答