MVC3 验证问题

本文关键字:问题 验证 MVC3 | 更新日期: 2023-09-27 18:31:35

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

@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; }
......

MVC3 验证问题

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

验证不起作用,因为使用 Html.TextAreaFor() 帮助程序来呈现"NewsContent"属性,

以下是使其工作的代码:

将模型更改为:

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

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>
//....