【发布时间】:2013-12-10 16:33:34
【问题描述】:
我在我们的网站上有一个简单的反馈表。当用户在页面中输入 html 时,他们应该会收到一条验证错误消息。但他们没有。异常被抛出并被捕获:
void Application_Error(object sender, EventArgs e)
{
...
}
这里捕获的异常是:
从 客户
在 System.Web.HttpRequest.ValidateString(字符串值,字符串 collectionKey, RequestValidationSource requestCollection)
这是 Asp.net MVC 标记:
<form action="<%=Url.Action("Create") %>" method="post" class="data-entry-form">
<fieldset class="comment">
Your thoughts:
<div class="editor-field">
<%= Html.TextAreaFor(model => model.Comment, 10, 2, new { placeholder="your message" }) %>
<%= Html.ValidationMessageFor(model => model.Comment) %>
</div>
<br />
E-mail address (optional)
<div class="editor-field">
<%= Html.TextBoxFor(model => model.Email, new { placeholder="you@youremailaddress.com" }) %>
<%= Html.ValidationMessageFor(model => model.Email) %>
</div>
<input type="submit" value="Send" />
</fieldset>
</form>
这是生成的html:
<form action="/feedback/Create/" method="post" class="data-entry-form">
<fieldset class="comment">
Your thoughts:
<div class="editor-field">
<textarea cols="2" id="Comment" name="Comment" placeholder="your message" rows="10">
</textarea>
</div>
<br />
E-mail address (optional)
<div class="editor-field">
<input id="Email" name="Email" placeholder="you@youremailaddress.com" type="text" value="" />
</div>
<input type="submit" value="Send" />
</fieldset>
</form>
这是控制器代码:
[HttpPost]
public ActionResult Create(Feedback feedback)
{
if (ModelState.IsValid)
{
FillFeedbackObject(feedback);
feedbackRepository.AddFeedback(feedback);
return View("SuccessMessage", new SuccessMessageModel("Thanks for your message!"));
}
return View(feedback);
}
我在 web.config 文件的 appSettings 部分中也有 <add key="ClientValidationEnabled" value="true" />,其中包括 jquery.validate.js。所以客户端和服务器端验证都失败了。
怎么了?
【问题讨论】:
标签: c# asp.net-mvc validation