Asp.net mvc asp net mvc部分视图验证

Asp.net mvc asp net mvc部分视图验证,asp.net-mvc,validation,partial-views,Asp.net Mvc,Validation,Partial Views,嗨,你好吗?我正在尝试验证ASP NET MVC中的表单 我有一个局部视图“地址”,我将其重新用于某些实体,如公司、个人等 我的问题是,当我提交表单时,只有父视图中的控件得到验证,而部分视图中的控件没有得到验证 这里有一些代码,我希望你能帮我 个人视图 @model Entities.Person @using (Html.BeginForm("Create", "Person", FormMethod.Post)) { <table> <tr&g

嗨,你好吗?我正在尝试验证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>
@model Entities.Person
@使用(Html.BeginForm(“Create”、“Person”、FormMethod.Post))
{
@LabelFor(model=>model.FirstName)
@Html.TextBoxFor(model=>model.FirstName,新的{@maxlength=7,@class=“numeric”})
@Html.ValidationMessageFor(model=>model.FirstName)
@LabelFor(model=>model.LastName)
@Html.TextBoxFor(model=>model.LastName,新的{@maxlength=7,@class=“numeric”})
@Html.ValidationMessageFor(model=>model.LastName)
@{Html.RenderAction(“Index”,“Address”,new{id=Model.AddressId});}//呈现地址表单部分
创造
取消
}
地址视图

@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>
@模型实体。地址
@LabelFor(model=>model.Street)
@文本框(“Address.Street”,Model.Street)
@Html.ValidationMessage(“Address.Street”)
@LabelFor(model=>model.Number)
@文本框(“Address.Number”,Model.Number)
@Html.ValidationMessage(“Address.Number”)

然后我为person和address字段提供了一些验证元数据([必需])

尝试使用
Html.Partial(…)
而不是
Html.RenderAction(…)
来呈现部分视图。这可能会抑制验证。

尝试使用
Html.Partial(…)
而不是
Html.RenderAction(…)
来呈现部分视图。这可能会抑制验证。

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

@Html.Partial([ViewName], [EmailAddressObject], ViewData)

使用
@Html.Partial(…)
并期望出现验证错误的常见错误是未将ViewData传递给该分部:

@Html.Partial([ViewName], [EmailAddressObject], ViewData)
试试这个:

@Html.EditorFor(Model.Street)
@Html.ValidationMessageFor(Model.Street)
与此相反:

@Html.TextBox("Address.Street", Model.Street)
@Html.ValidationMessage("Address.Street")
试试这个:

@Html.EditorFor(Model.Street)
@Html.ValidationMessageFor(Model.Street)
与此相反:

@Html.TextBox("Address.Street", Model.Street)
@Html.ValidationMessage("Address.Street")
型号

İimportant:管理Nuget包

=>>JQuery.Validation

=>>Microsoft.jqueryunobstrusive.Validation

安装

1)第一步

使用System.ComponentModel.DataAnnotations

2)

3)

if (model != null && ModelState.IsValid)
                {
                    var categoryCreate = new Categories
                    {
                       //code
                    };
                    _CategoriesService.Create(categoryCreate);
                }
4) 添加视图模型 @model Models.CategoryViewModel 之后

型号

İimportant:管理Nuget包

=>>JQuery.Validation

=>>Microsoft.jqueryunobstrusive.Validation

安装

1)第一步

使用System.ComponentModel.DataAnnotations

2)

3)

if (model != null && ModelState.IsValid)
                {
                    var categoryCreate = new Categories
                    {
                       //code
                    };
                    _CategoriesService.Create(categoryCreate);
                }
4) 添加视图模型 @model Models.CategoryViewModel 之后


您是否尝试过将{Html.RenderAction…}替换为@Html.Partial(“地址”)您是否尝试过将{Html.RenderAction…}替换为@Html.Partial(“地址”)在使用RenderAction之前,我尝试过Html.RenderPartial,但结果相同。这和Html.Partial是一样的,不是吗?在使用RenderAction之前,我已经尝试了Html.RenderPartial,并获得了相同的结果。这和Html是一样的。是吗?谢谢你的帮助。问题是,当您仅使用两个第一个参数(viewname和通常是model)调用partial时,它工作得很好(因此我猜ViewData是隐式传递的)。但是,如果您传递某个自定义内容(例如更改部分输出的标志),并且不附加现有的ViewData,则会丢失一些信息。在我的例子中,输入验证错误类没有应用于部分包含验证错误的输入。谢谢您的帮助。问题是,当您仅使用两个第一个参数(viewname和通常是model)调用partial时,它工作得很好(因此我猜ViewData是隐式传递的)。但是,如果您传递某个自定义内容(例如更改部分输出的标志),并且不附加现有的ViewData,则会丢失一些信息。在我的例子中,输入验证错误类没有应用到包含部分验证错误的输入。