即使数据不好,Ajax.BeginForm也会触发OnSuccess方法

时间:2012-12-05 19:51:52

标签: asp.net-mvc forms razor ajax.beginform

我试图将表单提交给控制器操作并根据OnSuccess或OnFailure处理响应。问题是,即使数据无效且我没有通过ModelState.IsValid测试,也会调用OnSuccess方法。应该调用OnFailure方法。

我的观点:

@using (Ajax.BeginForm("UpdateCategory", "Home", null, new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "categoryForm", OnSuccess = "alert('success');", OnFailure = "alert('failure');" }, new { id = "formEditCategory" }))
{
    @Html.ValidationSummary(true)

    @Html.HiddenFor(model => model.CategoryID)

    <div>
        <div class="editor-label">
            @Html.LabelFor(model => model.CategoryName)
        </div>
        <div class="small-multiline-field">
            @Html.EditorFor(model => model.CategoryName)
        </div>
        <div class="validationMsg">
            @Html.ValidationMessageFor(model => model.CategoryName)
        </div>
    </div>
}

我的控制器操作:

[HttpPost]
public ActionResult UpdateCategory(CategoryVM category)
{
    try
    {
        if (ModelState.IsValid)
        {
            var itemService = new ItemViewModels();
            itemService.UpdateCategory(category);
        }
    }
    catch (DataException)
    {
        //Log the error (add a variable name after DataException)
        ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
    }

    return PartialView("EditCategoryInfo", category);
}

我的ViewModel:

public class CategoryVM
{
    public int CategoryID { get; set; }

    [StringLength(75, ErrorMessage = "Category Name must be under 75 characters.")]
    [DataType(DataType.MultilineText)]
    [Display(Name = "Name")]
    public string CategoryName { get; set; }

    [StringLength(3800, ErrorMessage = "Category Description must be under 3800 characters.")]
    [DataType(DataType.MultilineText)]
    [Display(Name = "Description")]
    [AllowHtml]
    public string CategoryDesc { get; set; }

    [Display(Name = "Display on Web")]
    public bool DisplayOnWeb { get; set; }
}

因此,如果我在CategoryName字段中输入超过75个字符的字符串,我可以看到该表单未通过ModelState.IsValid测试,并且使用&#34发回带有视图的视图;类别名称必须低于75字符&#34;错误信息。但是它不是触发OnFailure事件,而是触发OnSuccess事件。为什么呢?

提前致谢。

3 个答案:

答案 0 :(得分:6)

由于您已捕获异常,因此返回的是具有HTTP状态代码200的PartialView。这就是触发OnSuccess的原因。

您可以做的是将响应代码显式设置为400(错误请求)

[HttpPost]
public ActionResult UpdateCategory(CategoryVM category)
{
    try
    {
        if (ModelState.IsValid)
        {
            var itemService = new ItemViewModels();
            itemService.UpdateCategory(category);
        }
    }
    catch (DataException)
    {
        //Log the error (add a variable name after DataException)
        ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
        Response.StatusCode = 400;
    }

    return PartialView("EditCategoryInfo", category);
}

答案 1 :(得分:2)

如果服务器返回成功的HTTP响应,则调用

OnSuccess方法,例如200 OK,如果客户端成功收到响应并更新页面(如MSDN所述)

由您来处理回复的内容。

答案 2 :(得分:1)

通过更多的研究(以及Lukas的帮助),我发现了this,这解决了我的问题。