1

我有以下视图,无法验证 Title 和 NewsContent。标题验证有效,但 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; }
......
4

1 回答 1

3

问题:标题验证有效,但 NewsContent 无效。

验证不起作用,因为使用 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>
//....
于 2012-04-18T18:35:44.560 回答