gpt4 book ai didi

c# - Ajax.BeginForm OnSuccess 未触发

转载 作者:太空狗 更新时间:2023-10-29 17:31:59 30 4
gpt4 key购买 nike

我正在使用这个局部 View

@model CreateConfigEntityModel

<div class="row">
@using (Ajax.BeginForm("AddElement", "MerchantSites", new { merchantId = @Model.MerchantId }, new AjaxOptions
{
HttpMethod = "POST",
OnSuccess = "alert('ok')"
},
new { id = "addConfigForm" }
))
{
@Html.LabelFor(m => m.EntityName)
@Html.TextBoxFor(m => m.EntityName)
@Html.ValidationMessageFor(m => m.EntityName)

@Html.LabelFor(m => m.DefaultValue)
@Html.TextBoxFor(m => m.DefaultValue)
@Html.ValidationMessageFor(m => m.DefaultValue)

<input type="submit" value="Ajouter" class="tiny button" />
}
</div>

Controller

public JsonResult AddElement(CreateConfigEntityModel model)
{
if (ModelState.IsValid)
{
_merchantSitesManager.AddEntity(model.EntityName, model.DefaultValue);
return Json(new { code = 1 }, JsonRequestBehavior.AllowGet);
}
else
return Json(new { code = 0 }, JsonRequestBehavior.AllowGet);
}

这是提交表单后显示的内容(项目已正确添加)

enter image description here

不确定我做错了什么。

使用 jQuery JavaScript 库 v2.1.1

我的 web.config 中有

<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

我这样调用我的局部 View

@{ Html.RenderPartial("_CreateConfigEntity", new CreateConfigEntityModel(Model.MerchantId)); }

最佳答案

假设您有一个全新的项目,您需要执行以下操作才能使其正常运行。 ASP.NET MVC 模板开箱即用地支持不显眼的 AJAX:

  1. 添加"Microsoft.jQuery.Unobtrusive.Ajax"从 Nuget 打包到您的项目中。您可以通过右键单击项目并选择“管理 Nuget 包”来执行此操作。
  2. 将“jquery.unobtrusive-ajax.js”添加到您的页面。如果您在 System.Web.Optimization 中使用“捆绑”功能,一种简单的方法是将其添加到 jQuery bundle 中:

    bundles.Add(new ScriptBundle("~/bundles/jquery")
    .Include("~/Scripts/jquery-{version}.js")
    .Include("~/Scripts/jquery.unobtrusive-ajax.js"));

    您也可以只添加 <script>指向脚本的标记。

假设页面正在加载 jQuery jquery.unobtrusive-ajax.js,您发布的代码应该可以工作。

关于c# - Ajax.BeginForm OnSuccess 未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26362134/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com