ASP.NET MVC验证的问题 - 数据未公布 [英] ASP.NET MVC validation problem - data is not posted
问题描述
我有以下类:
public class NewCommentClass
{
public string ActionName { get; set; }
public object RouteValues { get; set; }
[Required(ErrorMessage = "Comment Required")]
public string Comment { get; set; }
public int? CommentParentID { get; set; }
}
以下code鉴于:
following code in view:
NewCommentClass newCommentClass = new NewCommentClass() { ActionName = "PostComment", RouteValues = new { id = ideaItem.Ideas.IdeaID } };
Html.RenderPartial("~/Views/Shared/NewComment.ascx", newCommentClass);
和NewComment.ascx:
and NewComment.ascx:
<% @ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<NEOGOV_Ideas.Models.NewCommentClass>" %>
....
<div class="comment-new-container">
<div class="grid_1 alpha item-sidebar">
<p style="padding-top: 0.5em">
<a href="#">
<img src="<% = userAvatar %>" class="profile-photo" alt="Your Profile Picture" width="48"
height="48" /></a>
</p>
</div>
<div class="grid_8 omega">
<div class="comment-body">
<% using (Html.BeginForm(Model.ActionName, "Home", Model.RouteValues, FormMethod.Post, new { id = "FormAddComment", name = "FormAddComment" }))
{ %>
<fieldset>
<% = Html.TextAreaFor(model => model.Comment, htmlAttributes)%>
<% = Html.ValidationMessageFor(model=>model.Comment) %>
<input type="submit" value="<% = postButtonTitle %>" class="small blue awesome noborder" />
</fieldset>
<%} %>
</div>
</div>
<div class="clear">
</div>
</div>
和在控制器下面的帖子方法:
and following post method in controller:
public ActionResult PostComment(int id, string Comment, int? CommentParentID, string referrerUrl)
{
...
}
但这个验证工作不正常。
如果我输入的数据textarea的,然后点击提交 - 一切ok
但如果我只是点击提交无内部的数据 - 得到错误信息(这是正确的),但是当我这个动作后,输入数据,文本区域 - 错误信息已被隐藏,但形式不是submited!如果我添加Html.ValidationSummary(真) - 我一个标签是隐藏的,但第二个所示。
为什么这么奇怪的行为?
but this validation does not work correctly. If I enter data to textarea and click on "Submit" - all ok But If I just click on "Submit" without data inside - got error message (it's correct), but when I enter data to textarea after this action - error message is hidden, but form is not submited!. If I add Html.ValidationSummary(true) - I one label is hidden, but second is shown. Why so strange behaviour?
推荐答案
在你的Html.BeginForm()命令,创建一个HtmlAttribute对象,你用它的名字和你的textarea的ID设置为 FormAddComment
。因为这是在表单的唯一领域,你需要改变你的方法的签名如下:
In your Html.BeginForm() command, you create an HtmlAttribute object, and you use it to set the name and id of your textarea to FormAddComment
. Because this is the only field in the form, you would need to change your method signature as follows:
[HttpPost]
public ActionResult PostComment(string FormAddComment)
您当前的签名没有收到从发布的形式什么。如果您使用Fiddler或类似的工具来检查什么被贴出来,你会看到 FormAddComment = [无论是输入到文本区域]
作为POST的身体发出你的浏览器。
Your current signature doesn't receive anything from the posted form. If you use Fiddler or a similar tool to inspect what is being posted, you will see FormAddComment=[whatever was typed into the textarea]
as the body of the POST sent from your browser.
这篇关于ASP.NET MVC验证的问题 - 数据未公布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!