MVC3验证问题

时间:2012-04-18 14:57:37

标签: c# asp.net-mvc-3 validation

我有以下视图,无法在Title和NewsContent上验证。标题验证有效但新闻内容无效。我该如何解决呢?

@model Foo.NewsViewModel
@{
    ViewBag.Title = "Create";
}



@using (Html.BeginForm("Create", "News", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div>
        <fieldset>
            <legend>Category Information</legend>
            <div class="editor-label">
                @Html.LabelFor(m => m.News.Title)
            </div>

            <div class="editor-field">
                @Html.TextBoxFor(m => m.News.Title)
                @Html.ValidationMessageFor(m => m.News.Title)
            </div>
               <div class="editor-label">
                @Html.LabelFor(m => m.News.NewsContent)
            </div>

            <div class="editor-field" id="container">
                @Html.TextAreaFor(m => m.News.NewsContent)
                @Html.ValidationMessageFor(m => m.News.NewsContent)
            </div>
            <div class="editor-label">
                @Html.LabelFor(m => m.News.Thumbnail)
            </div>

            <div class="editor-field">
                <input type="file" name="files" id="thumbnail" />
            </div>

            <div class="editor-label">
                @Html.LabelFor(m => m.News.Image)
            </div>

            <div class="editor-field">
                <input type="file" name="files" id="original" />
            </div>

            <div class="editor-label">
                @Html.Label("SelectedCategoryId")
            </div>

            <div class="editor-field">
                @Html.DropDownListFor(m => m.SelectedCategoryId, Model.Categories)
            </div>

            <div class="editor-label">
                Publish
            </div>

            <div class="editor-field">
                @Html.CheckBoxFor(m => m.News.Published, new { @checked = "checked" })
            </div>

            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
    </div>
}

这是模型|:

 public class News : IStorable
    {
        [Required]
        [Display(Name = "Title")]
        public virtual string Title { get; set; }

        [Required]
        [Display(Name = "Content")]
        public virtual string NewsContent { set; get; }
......

1 个答案:

答案 0 :(得分:3)

问题:标题验证有效但新闻内容无效。

验证不起作用,因为使用Html.TextAreaFor()帮助器来呈现“NewsContent”属性,

以下是使其有效的代码:

将您的模型更改为:

使用[DataType]属性装饰'NewsContent'属性,并将数据类型设置为'MultilineText'。这将指示此属性的编辑器应为多行文本输入。

public class News : IStorable
{
    [Required]
    [Display(Name = "Title")]
    public virtual string Title { get; set; }

    [Required()]
    [Display(Name = "Content")]
    [DataType(DataType.MultilineText)]
    public virtual string NewsContent { set; get; }
    //....
}

在视图中使用Html.EditorFor()帮助程序而不是Html.TextAreaFor()作为'News.NewsContent'属性。

//....
<div class="editor-label">
    @Html.LabelFor(m => m.News.NewsContent)
</div>

<div class="editor-field" id="container">

    @*@Html.TextAreaFor(m => m.News.NewsContent)*@

    @Html.EditorFor(m => m.News.NewsContent)
    @Html.ValidationMessageFor(m => m.News.NewsContent)
</div>
//....
相关问题