TextBoxFor()不生成验证标记 [英] TextBoxFor() not generating validation markup

查看:193
本文介绍了TextBoxFor()不生成验证标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQL Server 2012中,我有AWARD表,两列标题和月份。标题为varchar(256),并不能为NULL。 MONTH数据类型为int,可以为NULL。

使用VS2012 Ultimate和EF 5.0.0,该TextBoxFor帮手MVC4程序是不生产验证(数据-VAL =需要和数据-VAL-所需=所​​需的信息)为题columne以上,但在同样的看法,月是得到正确验证的标记。该的.edmx设计器显示的标题是不可为空,撞人后,自动生成AWARD.cs文件不具有 [必需] 属性为标题列。

我能试试吗?

  @model MyProject.Models.AWARD

@ {
    ViewBag.Title =添加奖;
    布局=〜/查看/共享/ _EditorLayout.cshtml;
}

@using(Html.BeginForm()){
    @ Html.ValidationSummary(真)
    <字段集>
        <传奇>添加奖< /传说>
        <表>
            &其中; TR>
                < TD>
                    @ Html.LabelFor(型号=> model.TITLE)
                < / TD>
                < TD>
                    @ Html.TextAreaFor(型号=> model.TITLE)
                    < BR />@Html.ValidationMessageFor(model => model.TITLE)
                < / TD>
            < / TR>
        &其中; TR>
            < TD>
                @ Html.LabelFor(型号=> model.MONTH)
            < / TD>
            < TD> @ Html.DropDownListFor(型号=> model.MONTH,新的SelectList(MyProject.Models.Helpers.Months,键,值),[未选中])
                < BR />@Html.ValidationMessageFor(model => model.MONTH)
            < / TD>
        < / TR>

            &其中; TR>
                < TD>
                    <输入类型=提交值=添加/>
                < / TD>
                < TD>
                    @ Html.ActionLink(取消,索引,空,新{@class =取消键})< / TD>
            < / TR>
        < /表>
    < /字段集>
}
 

解决方案

您真的不应该直接绑定你的意见,你的数据映射实体。您应该创建视图模型类从控制器包装传递,并从您的视图中的数据,然后填充您的数据对象。

您可以然后在您的视图模型执行所需的验证,而不会影响生成的映射类。

型号

 公共类AwardViewModel
{
    [必需的,StringLength(30)]
    公共字符串名称{获取;组; }
    ....
}
 

查看

  @model AwardViewModel

@using(Html.BeginForm()){
    @ Html.EditorFor(M => m.Title)
    @ Html.ValidationMessageFor(M => m.Title)
    ...
}
 

控制器

  [HttpPost]
公众的ActionResult创建(AwardViewModel模型)
{
    / *创建在实体方面新人奖,填充
       从模型并提交相关领域。 * /
}
 

I have a SQL Server 2012 in which I have AWARD table with two columns TITLE and MONTH. TITLE is varchar(256) and cannot be NULL. MONTH is int and can be NULL.

With VS2012 Ultimate and EF 5.0.0, the TextBoxFor helper in MVC4 app is not producing validation (data-val="required" and data-val-required="required message") for the TITLE columne above, but in the same View, MONTH is getting the correct validation markup. The .edmx designer does show TITLE is NonNullable, BUTT, the automatically generated AWARD.cs file does not have the [Required] attribute for the TITLE column.

What can I try?

@model MyProject.Models.AWARD

@{
    ViewBag.Title = "Add Award";
    Layout = "~/Views/Shared/_EditorLayout.cshtml";
}

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Add Award</legend>
        <table>
            <tr>
                <td>
                    @Html.LabelFor(model => model.TITLE)
                </td>
                <td>
                    @Html.TextAreaFor(model => model.TITLE)
                    <br />@Html.ValidationMessageFor(model => model.TITLE)
                </td>
            </tr>
        <tr>
            <td>
                @Html.LabelFor(model => model.MONTH)
            </td>
            <td>@Html.DropDownListFor(model => model.MONTH, new SelectList(MyProject.Models.Helpers.Months, "key","value"), "[Not Selected]")
                <br />@Html.ValidationMessageFor(model => model.MONTH)
            </td>
        </tr>

            <tr>
                <td>
                    <input type="submit" value="Add" />
                </td>
                <td>
                    @Html.ActionLink("Cancel", "Index", null, new { @class = "cancel-button" })</td>
            </tr>
        </table>
    </fieldset>
}

解决方案

You shouldn't really be binding your views directly to your data mapping entities. You should create view model classes to wrap the data you pass to and from your view and then populate your data objects from the controller.

You can then perform the required validation on your view model without affecting your generated mapping classes.

Model

public class AwardViewModel
{
    [Required, StringLength(30)]
    public string Title { get; set; }
    ....
}

View

@model AwardViewModel

@using (Html.BeginForm()) {
    @Html.EditorFor(m => m.Title)
    @Html.ValidationMessageFor(m => m.Title)
    ...
}

Controller

[HttpPost]
public ActionResult Create (AwardViewModel model)
{
    /* Create new AWARD in entity context, populate
       with relevant fields from model and commit. */
}

这篇关于TextBoxFor()不生成验证标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆