javascript - C# MVC JS 在 ajax 验证后无法在部分视图上工作/运行
问题描述
我有一个布局,在我的布局中,我调用了我认为的脚本代码。
@if (IsSectionDefined("VSscript"))
{
@RenderSection("VSscript");
}
我有一个视图,在我看来我有脚本部分@section VSscript {
我还有一个连接到部分视图的 ajax 部分 beginform
@using (Ajax.BeginForm("CreateServiceQuote", "Service", new AjaxOptions() { HttpMethod = "POST", UpdateTargetId = "ajaxCreate", OnSuccess="quoteSuccess" }))
在 ajax 部分我有我的部分代码
@Html.Partial("SQcreate")
我使用的所有 javascript 都在 VSscript 中,这在我看来。在局部视图中,我只有我需要的控件。当我第一次运行该项目时,一切正常。在我运行一组验证后,javascript 不再运行。我的 ajax onsuccess=""部分始终运行,但我的document.ready中所需的代码没有运行。这是我的控制器,如果它无效,它只是调用部分视图。
public ActionResult SQcreate()
{
var model = new ServiceModel();
return PartialView(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateServiceQuote(ServiceModel model)
{
if (ModelState.IsValid)
{
context.Serv_Quotes.Add(serviceQuote);
context.SaveChanges();
ModelState.Clear();
return Json("QuoteSuccess", JsonRequestBehavior.AllowGet);
}
return PartialView("SQcreate", serviceModel);
}
这是布局
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Titler</title>
@Styles.Render("~/Content/css")
@Styles.Render("~/Content/themes/base/css")
@Scripts.Render("~/bundles/modernizr")
@if (IsSectionDefined("VSscript"))
{
@*@RenderSection("VSscript");*@
@RenderSection("VSscript", required: false)
}
</head>
<body style="padding-top: 0px;">
<div class="container-fluid">
@RenderBody()
</div>
<!-- Script Bundle 1 -->
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jQueryValScripts")
<!-- ORRR Script Bundle 2 MINIFIED -->
<!--Scripts.Render("~/bundles/jQueryValScripts")-->
<!--*Scripts.Render("~/bundles/jqueryui")*-->
<!--*Scripts.Render("~/bundles/bootstrap")-->
<!--*Scripts.Render("~/bundles/unobtrusive")*-->
@*@RenderSection("VSscript", required: false)*@
</body>
</html>
和视图
@model PartNumbers.Models.ServiceModel
@{
HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
Layout = "~/Views/Shared/_LayoutPageI.cshtml";
ViewBag.Title = "Virtual Service";
//TempData["EmployeeID"] == null ? "" : TempData["EmployeeID"];
}
@section VSscript {
<script type="text/javascript">
$(function () {
$("#btnSQopenCon").on("click", function () {
$("#AddSQcontact").modal("show");
})
// jQuery UI Date Picker Class Object Name
$(".datepickerui").datepicker();
var modalQ = $('#AddQuote');
var custDDL = modalQ.find('.modal-body').find('#ServiceVM_CustomerName');
var sn2DDL = modalQ.find('.modal-body').find('#ServiceVM_SN2');
$(custDDL).change(function () {
//alert('testtesttest');
var cityName = $(custDDL).val();
var sn2Number = $(sn2DDL).val();
$.ajax({
url: '@Url.Action("SearchContactsList", "Service")',
data: { Customer: cityName, SN2: sn2Number },
datatype: "json",
success: function (data) {
//var dropdownlist = $('#ContactSearch');
var dropdownlist = modalQ.find('.modal-body').find('#ServiceVM_ContactName');
//$('#ServiceVM_ContactName');
dropdownlist.empty();
$.each(data, function () {
dropdownlist.append(
$('<option></option>').val(this.Value).html(this.Text)
);
});
}
});
});
})
function quoteSuccess(data) {
if (data == 'QuoteSuccess') {
$("#AddQuote").modal("hide");
window.location.reload();
//var quoteRefresh =
//$.ajax({
//})
}
function contactSuccess(data) {
if (data == "ContactSuccess") {
$("#AddSQcontact").modal("hide");
var modalQ = $('#AddQuote');
var custDDL = modalQ.find('.modal-body').find('#ServiceVM_CustomerName').val();
var sn2DDL = modalQ.find('.modal-body').find('#ServiceVM_SN2').val();
$.ajax({
url: '@Url.Action("SearchContactsList", "Service")',
data: { Customer: custDDL, SN2: sn2DDL },
datatype: 'json',
success: function (data) {
var dropdownlist = modalQ.find('.modal-body').find('#ServiceVM_ContactName');
dropdownlist.empty();
$.each(data, function () {
dropdownlist.append(
$('<option></option>').val(this.Value).html(this.Text)
);
});
}
});
</script>
}
<!-- CREATE Service Quote -->
@using (Ajax.BeginForm("CreateServiceQuote", "Service", new AjaxOptions() { HttpMethod = "POST", UpdateTargetId = "ajaxCreate", OnSuccess="quoteSuccess" }))
{
<div class="modal" id="AddQuote" tabindex="-1" role="dialog" aria-labelledby="lblAjaxCreate" aria-hidden="true">
<div class="modal-dialog" role="document" style="width:750px;">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title text-center"><b>New Service Quote</b></h3>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="ajaxCreate">
@Html.Partial("SQcreate")
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
<input type="submit" class="btn btn-success" value="Create Quote" />
</div>
</div>
</div>
</div>
}
<!-- CREATE CONTACT -->
@using (Ajax.BeginForm("CreateSQcontact", "Service", new AjaxOptions() { HttpMethod = "POST", UpdateTargetId = "ajaxContact", OnSuccess = "contactSuccess" }))
{
<div class="modal" id="AddSQcontact" tabindex="-1" role="dialog" aria-labelledby="lblAjaxContact" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h3 class="text-center"><b>Create Contact</b></h3>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="ajaxContact">
@Html.Partial("SQcontact")
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
<input type="submit" class="btn btn-success" value="Create Contact" />
</div>
</div>
</div>
</div>
}
解决方案
我对此的解决方案是查看开发工具中的“网络选项卡”。我发现在切换和重新加载视图时我的模型的某些部分没有被加载的错误。
推荐阅读
- javascript - 按键删除项目 - 如何将该键值传递给 React Native 中的 Delete 函数
- sql - Finding the number of agents working for a manager in a certain week
- c++ - 在 C++ 中包装浮点数计数器
- c - 使用具有固定宽度整数类型变量的 printf 宽度说明符
- tensorflow - 使用 MobileNetV1 的多位数分类器
- javascript - ReactNative:无法从 NativeModules 调用函数
- node.js - MEAN Stack:当 Angular 在 Node 文件夹中时,如何从 Node 启动 Angular
- javascript - 如何更改画布文本的字体大小
- rest-assured-jsonpath - RestAssured - JsonPath 表达式过滤器不起作用
- python - 如何按主题列出多个主题的订阅?