如何在Ajax表单中使用MVC3客户端表单验证?

如何在Ajax表单中使用MVC3客户端表单验证?,ajax,asp.net-mvc-3,validation,Ajax,Asp.net Mvc 3,Validation,整个事情是,我希望能够在自定义编写的Ajax代码上使用MVC3内置的客户端验证。(我说的是使用HTML.BeginForm()和Ajax.BeginForm()制作的表单中提供的验证) 我的模型如下所示: public class BrandVewModel { public int ID { get; set; } [Display(Name = "Brand Name")] [Required(ErrorMessage = "Plea

整个事情是,我希望能够在自定义编写的Ajax代码上使用MVC3内置的客户端验证。(我说的是使用
HTML.BeginForm()
Ajax.BeginForm()
制作的表单中提供的验证)

我的模型如下所示:

public class BrandVewModel
    {
        public int ID { get; set; }

        [Display(Name = "Brand Name")]
        [Required(ErrorMessage = "Please fill in the blank!")]
        [StringLength(40, ErrorMessage = "The name must not be more than 40 characters long!")]
        public string Name { get; set; }
    }
@using (Ajax.BeginForm("Insert_Brand", "Admin", FormMethod.Post, new AjaxOptions { UpdateTargetId = "result" }))
{
    @Html.HiddenFor(model => model.ID)
    @Html.ValidationMessageFor(model => model.Name)
    @Html.LabelFor(model => model.Name)
    @Html.TextBoxFor(model => model.Name)

    <input value="Create"  type="submit" name="Create New Brand"  />

    <div id="result"></div>
}
我的观点是这样的:

public class BrandVewModel
    {
        public int ID { get; set; }

        [Display(Name = "Brand Name")]
        [Required(ErrorMessage = "Please fill in the blank!")]
        [StringLength(40, ErrorMessage = "The name must not be more than 40 characters long!")]
        public string Name { get; set; }
    }
@using (Ajax.BeginForm("Insert_Brand", "Admin", FormMethod.Post, new AjaxOptions { UpdateTargetId = "result" }))
{
    @Html.HiddenFor(model => model.ID)
    @Html.ValidationMessageFor(model => model.Name)
    @Html.LabelFor(model => model.Name)
    @Html.TextBoxFor(model => model.Name)

    <input value="Create"  type="submit" name="Create New Brand"  />

    <div id="result"></div>
}
正如我所说,当我使用这种ajax表单发布时,我需要向用户显示所有良好的验证消息。但我不知道我怎样才能做到


有什么办法吗?

客户端验证在您的案例中不起作用的原因是您已经硬编码了
标记,因此没有任何东西可以实例化
FormContext
。如果没有FormContext,HTML帮助程序(如
TextBoxFor
)不会发出任何由不引人注目的框架使用的HTML5数据-*验证属性

因此,只需使用
Html.BeginForm
帮助程序:

@using (Html.BeginForm("Insert_Brand", "Admin"))
{
    @Html.HiddenFor(model => model.ID)
    @Html.ValidationMessageFor(model => model.Name)
    @Html.LabelFor(model => model.Name)
    @Html.TextBoxFor(model => model.Name)

    <a onclick="CreateBrand()">Create Brand</a>
}
然后在一个单独的javascript文件中:

$(function() {
    $('#myForm').submit(function() {

        var item = { Brand: { Name: $('#Name').val() } };
        $.ajax({
            url: this.action,
            type: this.method,
            data: JSON.stringify(item),
            contentType: 'application/json; charset=utf-8',
            success: function (t) {
                console.log(t);
                $('#result').html(t);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log("Failed!");
                console.log("XMLHttpRequest=" + XMLHttpRequest.responseText + "\ntextStatus=" + textStatus + "\nerrorThrown=" + errorThrown);
            }
        });

        return false;
    });
});
但是,如果出于某种隐晦的原因,您坚持手动硬编码此
标记,而不是使用为此目的而设计的帮助程序,您也可以通过在视图顶部添加以下行来手动创建FormContext:

@{
    ViewContext.FormContext = new FormContext();
}

从而诱使Html助手相信他们在表单中,并强制他们发出HTML5数据-*验证属性。

我不明白您想要实现什么,您目前面临哪些问题,以及您所展示的代码与您的问题有何关联。非常感谢Darin Dimitrov:)我会尝试一下,并告诉您结果。