1

你好你好吗?我正在尝试验证 ASP NET MVC 中的表单。

我有一个部分视图“地址”,可用于某些实体,如公司、个人等。

我的问题是,当我提交表单时,只有父视图的控件得到验证,部分视图中的控件没有。

这是一些代码,我希望你能帮我

人物视图

@model Entities.Person


@using (Html.BeginForm("Create", "Person", FormMethod.Post))
{

    <table>
        <tr>
            <td>
                @Html.LabelFor(model => model.FirstName)
                <div class="control">
                    @Html.TextBoxFor(model => model.FirstName, new { @maxlength = 7, @class = "numeric"})
                    @Html.ValidationMessageFor(model => model.FirstName)
                </div>
                <div class="spacer-short"></div>
            </td>
            <td>
                @Html.LabelFor(model => model.LastName)
                <div class="control">
                    @Html.TextBoxFor(model => model.LastName, new { @maxlength = 7, @class = "numeric"})
                    @Html.ValidationMessageFor(model => model.LastName)
                </div>
                <div class="spacer-short"></div>
            </td>
        </tr>
    </table>
    @{ Html.RenderAction("Index", "Address", new {id = Model.AddressId});} //Renders the Address form part

    <div class="spacer"></div>
    <button type="submit" data-button="true" data-button-icon="ui-icon-disk" class="">Create</button>
    <button type="button" data-button="true" data-button-icon="ui-icon-circle-close" class="button-cancel">Cancel</button>
}

地址视图

@model Entities.Address


<table>
     <tr>
         <td>
            @Html.LabelFor(model => model.Street)
            <div class="control">
                @Html.TextBox("Address.Street", Model.Street)
                @Html.ValidationMessage("Address.Street")
             </div>
           <div class="spacer-short"></div>
           </td>
           <td>
              @Html.LabelFor(model => model.Number)
              <div class="control">
                @Html.TextBox("Address.Number", Model.Number)
                @Html.ValidationMessage("Address.Number")
             </div>
           <div class="spacer-short"></div>
            </td>
        </tr>
    </table>

然后我有一些用于人员和地址字段的验证元数据([必需])

4

4 回答 4

8

使用@Html.Partial(...)并期望显示验证错误的一个常见错误是未将 ViewData 传递给该部分。:

@Html.Partial([ViewName], [EmailAddressObject], ViewData)
于 2013-03-18T19:07:18.040 回答
0

尝试使用Html.Partial(...)而不是Html.RenderAction(...)渲染局部视图。这可能会抑制验证。

于 2013-03-18T17:19:37.623 回答
0

试试这个:

@Html.EditorFor(Model.Street)
@Html.ValidationMessageFor(Model.Street)

而不是这个:

@Html.TextBox("Address.Street", Model.Street)
@Html.ValidationMessage("Address.Street")
于 2013-03-18T19:14:24.783 回答
0

模型

重要提示:管理 Nuget 包

=>> JQuery.Validation

=>> Microsoft.JQueryUnobtrusive.Validation

安装。

1) 第一步

使用 System.ComponentModel.DataAnnotations;

2)

    public int Id { get; set; }
    [Required(ErrorMessage = "* Kategori adı boş geçilemez.")]
    [DisplayName("Kategori Adı")]
    public string CategoryName { get; set; }
    [Required(ErrorMessage = "* Kategori açıklaması boş geçilemez.")]
    [DisplayName("Kategori Açıklaması")]
    public string Description { get; set; }

3)

if (model != null && ModelState.IsValid)
                {
                    var categoryCreate = new Categories
                    {
                       //code
                    };
                    _CategoriesService.Create(categoryCreate);
                }

4) 添加ViewModel @model Models.CategoryViewModel 之后

@Html.TextBoxFor(model => model.CategoryName, new { @class = "form-control input-sm", id = "categoryTitle", @placeholder = "Kategori adını giriniz.", @type= "text", @required=true })

@Html.ValidationMessageFor(model => model.CategoryName, "", new { @class = "error-message" })

@Html.TextBoxFor(model => model.Description, new { @class = "form-control input-sm", id = "categoryArticle", @placeholder = "Kategori açıklamasını giriniz.", @type = "text", @required = true })

@Html.ValidationMessageFor(model => model.Description, "", new { @class = "error-message" })
于 2018-02-26T14:04:55.397 回答