Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
BeginForm()将javascript变量设置为错误_Javascript_Jquery_Ajax_Asp.net Mvc - Fatal编程技术网

BeginForm()将javascript变量设置为错误

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

我有如下ajax表单:

@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),它工作了。。。因为结束(结果)无法识别结果。。。