C# MVC3验证问题
我有以下视图,它无法验证标题和新闻内容。标题验证有效,但新闻内容无效。我怎样才能修好它C# MVC3验证问题,c#,asp.net-mvc-3,validation,C#,Asp.net Mvc 3,Validation,我有以下视图,它无法验证标题和新闻内容。标题验证有效,但新闻内容无效。我怎样才能修好它 @model Foo.NewsViewModel @{ ViewBag.Title = "Create"; } @using (Html.BeginForm("Create", "News", FormMethod.Post, new { enctype = "multipart/form-data" })) { <div> <fieldset>
@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>
}
问题:标题验证有效,但新闻内容无效 验证无效,因为使用Html.TextAreaFor()帮助程序呈现“NewsContent”属性 以下是使其工作的代码: 将您的模型更改为: 用[DataType]属性修饰“NewContent”属性,并将数据类型设置为“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; }
//....
}
在视图中,对“News.newcontent”属性使用Html.EditorFor()帮助程序,而不是Html.TextAreaFor()
//....
<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>
//....
/。。。。
@LabelFor(m=>m.News.NewsContent)
@*@Html.TextAreaFor(m=>m.News.NewsContent)*@
@EditorFor(m=>m.News.NewsContent)
@Html.ValidationMessageFor(m=>m.News.NewsContent)
//....
是新闻内容丰富的文本区域,如FCKEditor等。现在只是文本区域。我将添加WYSIWYG您使用的实体框架“味道”是什么?它与实体框架有何关系/您能否详细说明哪些不起作用?“标题验证有效,但新闻内容无效”因此这意味着需要标题
,但您可以提交表单,而无需填写新闻内容
,或者其他问题?
//....
<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>
//....