gpt4 book ai didi

javascript - 在 asp.net mvc 中提交 Ajax.BeginForm 后 jQuery 代码无法正常工作

转载 作者:行者123 更新时间:2023-11-30 18:19:07 27 4
gpt4 key购买 nike

我试图将大问题最小化为一个小问题,因此我创建了新的示例 Web 项目; VS 中的 mvc-empty。我在 Home Controller 中创建了一个名为“Index”的 View 。索引查看代码:

@model WebApplication16.ViewModels.Home.IndexVM
@{
ViewBag.Title = "Index";
}

@Html.Partial("~/Views/Home/_Orders.cshtml", Model.Orders)

@section scripts{
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<script>
$("#Type").change(function () {



$('#order-current > img').remove();
var currentOrder = "#Type_" + $("#Type").find('option:selected').text();

var $img = $(currentOrder).clone();
$img.removeClass("hidden");
$("#order-current").append($img);

$("#ajax-form").submit();
});
</script>
}

家庭 Controller 代码:

public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
IndexVM dataVM = new IndexVM();
GetControlsDataSource(dataVM.Orders);

return View(dataVM);
}

private static void GetControlsDataSource(OrdersVM dataVM)
{
List<SelectListItem> typeControlDataSource = new List<SelectListItem>();
foreach (var en in Enum.GetValues(typeof(TypeEnum)))
{
SelectListItem item = new SelectListItem();
item.Text = en.ToString();
item.Value = ((int)en).ToString();
typeControlDataSource.Add(item);
}
dataVM.TypeControlDataSource = typeControlDataSource;
}


[HttpPost]
public ActionResult Pay(IndexVM dataVM)
{
GetControlsDataSource(dataVM.Orders);
if (ModelState.IsValid)
{
dataVM.Orders.Info = "Info bla bla bla";
return PartialView("~/Views/Home/_Orders.cshtml", dataVM.Orders);
}
else
{
return View(dataVM);
}

}
}

还有一个名为“_Orders”的局部 View ,它被渲染在Index View 上。_Orders局部 View 的代码:

@model WebApplication16.ViewModels.Home.OrdersVM

@using (Ajax.BeginForm("Pay", "Home", new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "result",
}, new { id = "ajax-form" }))
{
<div id="result">
<div id="order-current">

</div>


<div>
@Html.TextBoxFor(x => x.Name, new { @class = "form-control", style = "margin-top:10px;", id = "Name" })
@Html.ValidationMessageFor(x => x.Name)
</div>

<div>
@Html.DropDownListFor(x => x.Type, Model.TypeControlDataSource, new { @class = "form-control", style = "margin-top:10px;", id = "Type", })
@Html.ValidationMessageFor(x => x.Type)
</div>
<div>
<p>@Model.Info</p>
</div>
<button type="submit" class="btn btn-primary" name="ok"> OK</button>
</div>


}

<div id="orders-container">
<img id="Type_I" src="~/App_Images/Type_I.png" class="img-responsive hidden" />
<img id="Type_II" src="~/App_Images/Type_II.png" class="img-responsive hidden" />
<img id="Type_III" src="~/App_Images/Type_III.png" class="img-responsive hidden"/>
</div>
Index model is described by class IndexVM:
public class IndexVM
{
public IndexVM()
{
this.Orders = new OrdersVM();
}

public OrdersVM Orders { get; set; }
}

_Orders 模型由类 OrdersVM 描述:

public class OrdersVM
{

[Required]
public string Name { get; set; }

public string Info { get; set; }

[Required]
public TypeEnum Type { get; set; }

public List<SelectListItem> TypeControlDataSource { get; set; }
}


public enum TypeEnum
{
I,
II,
III
}

在 DropDownListFor 控件中更改 id=”Type” 的值后,来自隐藏字段的图片应该由位于索引 View 中的 jquery 代码注入(inject)到 id=”order-current” 的容器中,然后操作 ajax-form应该提交。它工作正常,但在调用

return PartialView("~/Views/Home/_Orders.cshtml", dataVM.Orders);

在 HomeController 中,字段 Info 已正确更新,但从“order-current”div 容器注入(inject)的图片消失了。我尝试使用 F12 按钮查看 Google Chrome 中的错误,没有错误,但在“browserLink”脚本中出现了无限循环。我无法解释为什么。我只想在提交 ajax-form 后在 id=”order-current 的容器中看到注入(inject)的图片。如何做到这一点以及我做错了什么?

最佳答案

感谢我的 friend ,我终于解决了这个问题。更新“结果”容器后,jQuery 添加到位于该容器中的控件的所有事件都将取消固定。这就是它崩溃的原因。使其正常工作的方法是创建一个函数并将其固定到 AjaxBeginForm 的事件 OnComplete 上。每次通过 ajax 更新结果容器后,都会调用此函数。我在 Home Controller 中也犯了一个小错误,因为我插入了一个错误的 View 模型类。全部改变后,它看起来像这样;家庭 Controller 代码:

public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
IndexVM dataVM = new IndexVM();
GetControlsDataSource(dataVM.Orders);

return View(dataVM);
}

private static void GetControlsDataSource(OrdersVM dataVM)
{
List<SelectListItem> typeControlDataSource = new List<SelectListItem>();
foreach (var en in Enum.GetValues(typeof(TypeEnum)))
{
SelectListItem item = new SelectListItem();
item.Text = en.ToString();
item.Value = ((int)en).ToString();
typeControlDataSource.Add(item);
}
dataVM.TypeControlDataSource = typeControlDataSource;
}


[HttpPost]
public ActionResult Pay(OrdersVM dataVM)
{
GetControlsDataSource(dataVM);
if (ModelState.IsValid)
{
dataVM.Info = "Info bla bla bla";
return PartialView("~/Views/Home/_Orders.cshtml", dataVM);
}
else
{
return View(dataVM);
}

}
}

索引 View :

@model WebApplication16.ViewModels.Home.IndexVM
@{
ViewBag.Title = "Index";
}

@Html.Partial("~/Views/Home/_Orders.cshtml", Model.Orders)

<div id="orders-container">
<img id="Type_I" src="~/App_Images/Type_I.png" class="img-responsive hidden" />
<img id="Type_II" src="~/App_Images/Type_II.png" class="img-responsive hidden" />
<img id="Type_III" src="~/App_Images/Type_III.png" class="img-responsive hidden" />
</div>

@section scripts{
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<script>

function imageOnChangeEvent() {
$("#ajax-form").submit();
}

function OnCompleteAjaxForm() {

$('#order-current > img').remove();
var currentOrder = "#Type_" + $("#Type").find('option:selected').text();

var $img = $(currentOrder).clone();
$img.removeClass("hidden");
$("#order-current").append($img);
}

</script>

}

_Orders 部分 View 代码:

@model WebApplication16.ViewModels.Home.OrdersVM

<div id="result">
@using (Ajax.BeginForm("Pay", "Home", new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "result",
OnComplete = "OnCompleteAjaxForm()"
}, new { id = "ajax-form" }))
{

<div id="order-current">

</div>


<div>
@Html.TextBoxFor(x => x.Name, new { @class = "form-control", style = "margin-top:10px;", id = "Name" })
@Html.ValidationMessageFor(x => x.Name)
</div>

<div>
@Html.DropDownListFor(x => x.Type, Model.TypeControlDataSource, new { @class = "form-control", style = "margin-top:10px;", id = "Type", onchange = "imageOnChangeEvent()" })
@Html.ValidationMessageFor(x => x.Type)
</div>
<div>
<p>@Model.Info</p>
</div>
<button type="submit" class="btn btn-primary" id="button_ok" name="ok"> OK</button>


}
</div>

关于javascript - 在 asp.net mvc 中提交 Ajax.BeginForm 后 jQuery 代码无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39644675/

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