繁体   English   中英

MVC3验证问题

[英]MVC3 validation problems

我有以下视图,无法验证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; }
......

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

验证不起作用,因为使用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>
//....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM