[英]ASP.NET MVC 2 server-side validation for ajax form
I've faced the following problem. 我遇到了以下问题。 I'm developing a form for the site and this form should have validation.
我正在为网站开发一个表单,这个表单应该有验证。 I wanna to use native ASP.NET MVC 2 validation functionality but get stubborn with it.
我想使用本机ASP.NET MVC 2验证功能,但对它很顽固。 I have a form that is loaded via $.get and displayed using jQuery UI modal dialog.
我有一个通过$ .get加载的表单,并使用jQuery UI模式对话框显示。 All examples I found explains how to use MVC validation with simple forms and avoid Ajax forms.
我找到的所有示例都解释了如何使用简单形式使用MVC验证并避免使用Ajax表单。
I can enable client side validation for this form, but I need to handle server-side validation correctly. 我可以为此表单启用客户端验证,但我需要正确处理服务器端验证。 How can I handle server-side validation model errors for ajax forms?
如何处理ajax表单的服务器端验证模型错误?
When you pass your object back to the controller, you have to wrap your code in If ModelState.IsValid
将对象传递回控制器时,必须将代码包装在
If ModelState.IsValid
Below is a simplified version of how I edit a user. 以下是我编辑用户的简化版本。 The first "EDIT" sends the User object TO the View.
第一个“EDIT”将User对象发送给View。 The second "EDIT" handles the post from the view.
第二个“编辑”处理视图中的帖子。
Function Edit() As ActionResult
''# do stuff to populate your User
Return View(User)
End Function
<AcceptVerbs(HttpVerbs.Post)> _
Function Edit(ByVal user as User)
If ModelState.IsValid Then
''# do your valid stuff
Else
''# The posted form was not valid, send the user back
Return View(user)
End If
End Function
Here's the same thing in C# 这是C#中的相同内容
public ActionResult Edit()
{
// do stuff to populate your User
return View(User);
}
[AcceptVerbs(HttpVerbs.Post)]
public object Edit(User user)
{
if (ModelState.IsValid) {
// do your valid stuff
} else {
//'# The posted form was not valid, send the user back
return View(user);
}
}
On your view, if you want to add AJAX validation, just add the following. 在您的视图中,如果要添加AJAX验证,只需添加以下内容即可。
<%
Html.EnableClientValidation() ''# This is where all the magic happens. It will build your clientside validation for you out of your MetaData.
Using Html.BeginForm("Edit", "Users")
%>
<!-- all your markup crap -->
<tr>
<td>
<%: Html.LabelFor(Function(model) model.UserName)%></td>
<td>
<%: Html.TextBoxFor(Function(model) model.UserName) %>
<%: Html.ValidationMessage("UserName", "*")%><br />
</td>
</tr>
<!-- somewhere you'll want to add a Validation Summary of all your errors -->
<%= Html.ValidationSummary("Oops!, please correct the errors...") %>
<% End Using%>
<!-- bottom of the page -->
<script src="../../Assets/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Assets/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="../../Assets/Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
Here is some info on rendering using Ajax.BeginForm
以下是使用
Ajax.BeginForm
进行渲染的一些信息
http://singulartechnologies.com/asp-net-mvc-ajax-beginform-sample-code http://singulartechnologies.com/asp-net-mvc-ajax-beginform-sample-code
http://msdn.microsoft.com/en-us/library/dd381533.aspx http://msdn.microsoft.com/en-us/library/dd381533.aspx
http://weblogs.asp.net/mikebosch/archive/2008/02/15/asp-net-mvc-submitting-ajax-form-with-jquery.aspx http://weblogs.asp.net/mikebosch/archive/2008/02/15/asp-net-mvc-submitting-ajax-form-with-jquery.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.