C# MVC Ajax 模式验证 - 500 内部服务器错误
C# MVC Ajax modal validation - 500 internal server error
我不太确定为什么这不起作用,我在项目的不同部分有完全相同的东西,我得到一个“POST”500(内部服务器错误)我只是只是试图获得@using (Ajax.BeginForm( 验证以在模态上工作。就像我说的那样,我在项目的不同部分工作并且它有效。我的模态成功填充但是当我单击提交时按钮我得到上面的错误。在我看来,模态和模态的控件我都有
HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
我的代码中包含所有这些库。
这是脚本部分
<script src="/Scripts/jquery-3.3.1.js"></script>
<script src="/Scripts/jquery-ui-1.12.1.js"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="/Scripts/jquery.validate.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="/Scripts/bootstrap.js"></script>
<script src="/Scripts/respond.js"></script>
@using (Ajax.BeginForm("PartNumberUpdate", "Parts", new AjaxOptions() { HttpMethod = "POST", UpdateTargetId = "PartNumControls", OnSuccess = "ajaxPartUpdate" }))
{
<div class="modal" id="modalPNUpdate" tabindex="-1" role="dialog" aria-labelledby="lblPNUpdate" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title">Part Number Details</h3>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="PartNumControls">
@Html.Partial("PNControls")
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
<input type="submit" class="btn btn-primary" value="Save" />
</div>
</div>
</div>
</div>
}
这是控制器代码
[HttpPost]
[ValidateAntiForgeryToken]
[HandleError]
public ActionResult PartNumberUpdate(FindPartModel model)
{
if (ModelState.IsValid)
{
var partNumber = context.PartNumbers.Where(x => x.PartNumber1 == model.PartVM.PartNumber).FirstOrDefault();
partNumber.PartNumber1 = model.PartVM.PartNumber;
/// UPDATE PartNumber Record
context.Entry(partNumber).State = EntityState.Modified;
context.SaveChanges();
string returnStr = "refresh";
ModelState.Clear();
return Json(returnStr);
}
return PartialView("PNControls", model);
}
public ActionResult PNControls()
{
return View(new FindPartModel());
}
已添加 - 这是部分视图 'PNControls'
@model Messer_PartNumbers.Models.FindPartModel
@{
HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
}
@Html.AntiForgeryToken()
<span class="alert-danger">
@Html.ValidationSummary(true, "", new { @class="text-danger" })
</span>
@Html.HiddenFor(x => x.PartVM.PartID)
@Html.HiddenFor(x => x.PartVM.PartGroup)
@Html.HiddenFor(x => x.PartVM.GlobalPart)
@Html.HiddenFor(x => x.PartVM.Released)
<div class="form-group">
@Html.LabelFor(x =>x.PartVM.PartNumber, htmlAttributes: new { @class="control-label col-3" })
<div class="col-9">
@Html.TextBoxFor(x => x.PartVM.PartNumber, new { @class="form-control", @readonly="readonly" })
@Html.ValidationMessageFor(x => x.PartVM.PartNumber, "", new { @class="text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(x => x.PartVM.EnteredBy, htmlAttributes: new { @class = "control-label col-3" })
<div class="col-9">
@*@Html.TextBoxFor(x => x.PartVM.EnteredBy, new { @class="form-control" })*@
@Html.DropDownListFor(x=>x.PartVM.EnteredBy, Model.PNEnteredByOptions, new { @class="form-control"})
@Html.ValidationMessageFor(x => x.PartVM.EnteredBy, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(x => x.PartVM.DateEntered, htmlAttributes: new { @class = "control-label col-3" })
<div class="col-9">
@Html.TextBoxFor(x => x.PartVM.DateEntered, new { @class = "form-control", @readonly = "readonly" })
@Html.ValidationMessageFor(x => x.PartVM.DateEntered, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(x => x.PartVM.MachineTypes, htmlAttributes: new { @class = "control-label col-3" })
<small>Hold Ctrl to select multiples</small>
<div class="col-9">
@*@Html.TextBoxFor(x => x.PartVM.MachineTypes, new { @class = "form-control" })*@
@Html.ListBoxFor(x => x.PartVM.MachineTypes, Model.PNMachineTypeOptions, new { @class = "form-control", @size = 5, @style = "overflow:scroll;" })
@Html.ValidationMessageFor(x => x.PartVM.MachineTypes, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(x => x.PartVM.Description, htmlAttributes: new { @class="control-label col-3" })
<div class="col-9">
@Html.TextBoxFor(x => x.PartVM.Description, new { @class="form-control" })
@Html.ValidationMessageFor(x => x.PartVM.Description, "", new { @class="text-danger" })
</div>
</div>
[HttpPost]
[ValidateAntiForgeryToken]
[HandleError]
public ActionResult PartNumberUpdate(FindPartModel model){/*skip*/}
您在操作的基础上添加了 [ValidateAntiForgeryToken]
。
但是您没有 @Html.AntiForgeryToken()
在您的视图中。
@using (Ajax.BeginForm("PartNumberUpdate", "Parts", new AjaxOptions() { HttpMethod = "POST", UpdateTargetId = "PartNumControls", OnSuccess = "ajaxPartUpdate" })){
@Html.AntiForgeryToken() //Add me
//skip
}
我发现了我的错误。
这是对我的模型的自定义验证。正在使用其他型号。
谢谢@!我最终通过使用网络选项卡并在 'All' 中查看堆栈跟踪来找出更详细的错误。
post 让我产生了在开发人员工具网络选项卡中使用堆栈跟踪的想法。
500 Internal Server Error in ASP.NET MVC
我不太确定为什么这不起作用,我在项目的不同部分有完全相同的东西,我得到一个“POST”500(内部服务器错误)我只是只是试图获得@using (Ajax.BeginForm( 验证以在模态上工作。就像我说的那样,我在项目的不同部分工作并且它有效。我的模态成功填充但是当我单击提交时按钮我得到上面的错误。在我看来,模态和模态的控件我都有
HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
我的代码中包含所有这些库。
这是脚本部分
<script src="/Scripts/jquery-3.3.1.js"></script>
<script src="/Scripts/jquery-ui-1.12.1.js"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="/Scripts/jquery.validate.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="/Scripts/bootstrap.js"></script>
<script src="/Scripts/respond.js"></script>
@using (Ajax.BeginForm("PartNumberUpdate", "Parts", new AjaxOptions() { HttpMethod = "POST", UpdateTargetId = "PartNumControls", OnSuccess = "ajaxPartUpdate" }))
{
<div class="modal" id="modalPNUpdate" tabindex="-1" role="dialog" aria-labelledby="lblPNUpdate" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title">Part Number Details</h3>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="PartNumControls">
@Html.Partial("PNControls")
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
<input type="submit" class="btn btn-primary" value="Save" />
</div>
</div>
</div>
</div>
}
这是控制器代码
[HttpPost]
[ValidateAntiForgeryToken]
[HandleError]
public ActionResult PartNumberUpdate(FindPartModel model)
{
if (ModelState.IsValid)
{
var partNumber = context.PartNumbers.Where(x => x.PartNumber1 == model.PartVM.PartNumber).FirstOrDefault();
partNumber.PartNumber1 = model.PartVM.PartNumber;
/// UPDATE PartNumber Record
context.Entry(partNumber).State = EntityState.Modified;
context.SaveChanges();
string returnStr = "refresh";
ModelState.Clear();
return Json(returnStr);
}
return PartialView("PNControls", model);
}
public ActionResult PNControls()
{
return View(new FindPartModel());
}
已添加 - 这是部分视图 'PNControls'
@model Messer_PartNumbers.Models.FindPartModel
@{
HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
}
@Html.AntiForgeryToken()
<span class="alert-danger">
@Html.ValidationSummary(true, "", new { @class="text-danger" })
</span>
@Html.HiddenFor(x => x.PartVM.PartID)
@Html.HiddenFor(x => x.PartVM.PartGroup)
@Html.HiddenFor(x => x.PartVM.GlobalPart)
@Html.HiddenFor(x => x.PartVM.Released)
<div class="form-group">
@Html.LabelFor(x =>x.PartVM.PartNumber, htmlAttributes: new { @class="control-label col-3" })
<div class="col-9">
@Html.TextBoxFor(x => x.PartVM.PartNumber, new { @class="form-control", @readonly="readonly" })
@Html.ValidationMessageFor(x => x.PartVM.PartNumber, "", new { @class="text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(x => x.PartVM.EnteredBy, htmlAttributes: new { @class = "control-label col-3" })
<div class="col-9">
@*@Html.TextBoxFor(x => x.PartVM.EnteredBy, new { @class="form-control" })*@
@Html.DropDownListFor(x=>x.PartVM.EnteredBy, Model.PNEnteredByOptions, new { @class="form-control"})
@Html.ValidationMessageFor(x => x.PartVM.EnteredBy, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(x => x.PartVM.DateEntered, htmlAttributes: new { @class = "control-label col-3" })
<div class="col-9">
@Html.TextBoxFor(x => x.PartVM.DateEntered, new { @class = "form-control", @readonly = "readonly" })
@Html.ValidationMessageFor(x => x.PartVM.DateEntered, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(x => x.PartVM.MachineTypes, htmlAttributes: new { @class = "control-label col-3" })
<small>Hold Ctrl to select multiples</small>
<div class="col-9">
@*@Html.TextBoxFor(x => x.PartVM.MachineTypes, new { @class = "form-control" })*@
@Html.ListBoxFor(x => x.PartVM.MachineTypes, Model.PNMachineTypeOptions, new { @class = "form-control", @size = 5, @style = "overflow:scroll;" })
@Html.ValidationMessageFor(x => x.PartVM.MachineTypes, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(x => x.PartVM.Description, htmlAttributes: new { @class="control-label col-3" })
<div class="col-9">
@Html.TextBoxFor(x => x.PartVM.Description, new { @class="form-control" })
@Html.ValidationMessageFor(x => x.PartVM.Description, "", new { @class="text-danger" })
</div>
</div>
[HttpPost]
[ValidateAntiForgeryToken]
[HandleError]
public ActionResult PartNumberUpdate(FindPartModel model){/*skip*/}
您在操作的基础上添加了 [ValidateAntiForgeryToken]
。
但是您没有 @Html.AntiForgeryToken()
在您的视图中。
@using (Ajax.BeginForm("PartNumberUpdate", "Parts", new AjaxOptions() { HttpMethod = "POST", UpdateTargetId = "PartNumControls", OnSuccess = "ajaxPartUpdate" })){
@Html.AntiForgeryToken() //Add me
//skip
}
我发现了我的错误。
这是对我的模型的自定义验证。正在使用其他型号。
谢谢@!我最终通过使用网络选项卡并在 'All' 中查看堆栈跟踪来找出更详细的错误。 post 让我产生了在开发人员工具网络选项卡中使用堆栈跟踪的想法。 500 Internal Server Error in ASP.NET MVC