BeginForm()将javascript变量设置为错误
我有如下ajax表单:BeginForm()将javascript变量设置为错误,javascript,jquery,ajax,asp.net-mvc,Javascript,Jquery,Ajax,Asp.net Mvc,我有如下ajax表单: @using (Ajax.BeginForm("AddMyModel", "MyModel", new AjaxOptions { HttpMethod = "POST", OnBegin = "begin", OnComplete = "end", UpdateTargetId = "addcontent", InsertionMode = InsertionMode.InsertAfter }, new { id = "v
@using (Ajax.BeginForm("AddMyModel", "MyModel",
new AjaxOptions
{
HttpMethod = "POST",
OnBegin = "begin",
OnComplete = "end",
UpdateTargetId = "addcontent",
InsertionMode = InsertionMode.InsertAfter
},
new
{
id = "validationForm",
@class = "edit_form",
style = "width: 400px;"
}))
{
<div id="addcontent">
@Html.Partial("_MyPartialView", Model)
</div>
}
可能吗?这听起来合理还是容易出错?您应该在
AjaxOptions
集合中设置OnFailure选项:
@using (Ajax.BeginForm("AddMyModel", "MyModel",
new AjaxOptions
{
HttpMethod = "POST",
OnBegin = "begin",
OnComplete = "end",
OnFailure = "handleError(error)",
UpdateTargetId = "addcontent",
InsertionMode = InsertionMode.InsertAfter
},
然后:
function handleError(error)
{
//error handling logic
}
以这种方式处理此类错误是非常常见的做法
如果你想自己决定一项行动是否成功,你可以写以下内容:
[HttpPost]
public ActionResult Process(MyViewModel model)
{
if (!ModelState.IsValid) // for example
{
return Json(new { success = false });
}
return Json(new { success = true });
}
然后修改OnSuccess
函数:
OnComplete = "end(result)"
function end(result) {
if (result.success) {
// executed when successful
} else {
// executed when you decided it is not successful
}
}
然后将错误检查作为success
功能的一部分:
OnComplete = "end(result)"
function end(result) {
if (result.success) {
// executed when successful
} else {
// executed when you decided it is not successful
}
}
您应该在
AjaxOptions
集合中设置OnFailure选项:
@using (Ajax.BeginForm("AddMyModel", "MyModel",
new AjaxOptions
{
HttpMethod = "POST",
OnBegin = "begin",
OnComplete = "end",
OnFailure = "handleError(error)",
UpdateTargetId = "addcontent",
InsertionMode = InsertionMode.InsertAfter
},
然后:
function handleError(error)
{
//error handling logic
}
以这种方式处理此类错误是非常常见的做法
如果你想自己决定一项行动是否成功,你可以写以下内容:
[HttpPost]
public ActionResult Process(MyViewModel model)
{
if (!ModelState.IsValid) // for example
{
return Json(new { success = false });
}
return Json(new { success = true });
}
然后修改OnSuccess
函数:
OnComplete = "end(result)"
function end(result) {
if (result.success) {
// executed when successful
} else {
// executed when you decided it is not successful
}
}
然后将错误检查作为success
功能的一部分:
OnComplete = "end(result)"
function end(result) {
if (result.success) {
// executed when successful
} else {
// executed when you decided it is not successful
}
}
您应该在
AjaxOptions
集合中设置OnFailure选项:
@using (Ajax.BeginForm("AddMyModel", "MyModel",
new AjaxOptions
{
HttpMethod = "POST",
OnBegin = "begin",
OnComplete = "end",
OnFailure = "handleError(error)",
UpdateTargetId = "addcontent",
InsertionMode = InsertionMode.InsertAfter
},
然后:
function handleError(error)
{
//error handling logic
}
以这种方式处理此类错误是非常常见的做法
如果你想自己决定一项行动是否成功,你可以写以下内容:
[HttpPost]
public ActionResult Process(MyViewModel model)
{
if (!ModelState.IsValid) // for example
{
return Json(new { success = false });
}
return Json(new { success = true });
}
然后修改OnSuccess
函数:
OnComplete = "end(result)"
function end(result) {
if (result.success) {
// executed when successful
} else {
// executed when you decided it is not successful
}
}
然后将错误检查作为success
功能的一部分:
OnComplete = "end(result)"
function end(result) {
if (result.success) {
// executed when successful
} else {
// executed when you decided it is not successful
}
}
您应该在
AjaxOptions
集合中设置OnFailure选项:
@using (Ajax.BeginForm("AddMyModel", "MyModel",
new AjaxOptions
{
HttpMethod = "POST",
OnBegin = "begin",
OnComplete = "end",
OnFailure = "handleError(error)",
UpdateTargetId = "addcontent",
InsertionMode = InsertionMode.InsertAfter
},
然后:
function handleError(error)
{
//error handling logic
}
以这种方式处理此类错误是非常常见的做法
如果你想自己决定一项行动是否成功,你可以写以下内容:
[HttpPost]
public ActionResult Process(MyViewModel model)
{
if (!ModelState.IsValid) // for example
{
return Json(new { success = false });
}
return Json(new { success = true });
}
然后修改OnSuccess
函数:
OnComplete = "end(result)"
function end(result) {
if (result.success) {
// executed when successful
} else {
// executed when you decided it is not successful
}
}
然后将错误检查作为success
功能的一部分:
OnComplete = "end(result)"
function end(result) {
if (result.success) {
// executed when successful
} else {
// executed when you decided it is not successful
}
}
回答得很好!非常感谢。我改变了结束(结果)到结束(xhr),它工作了。。。因为最后(结果)不承认结果…很好的答案!非常感谢。我改变了结束(结果)到结束(xhr),它工作了。。。因为最后(结果)不承认结果…很好的答案!非常感谢。我改变了结束(结果)到结束(xhr),它工作了。。。因为最后(结果)不承认结果…很好的答案!非常感谢。我改变了结束(结果)到结束(xhr),它工作了。。。因为结束(结果)无法识别结果。。。