Jquery Ajax在MVC AsP.net中发布表单(验证)?
所以我有一个页面,上面有一些表单,其中一个表单是地址表单,它有三个功能。另存为新建、更新和删除。我想使用form.submit()在客户端和服务器端启动MVC验证,但我不想刷新整个页面 这就是我想要使用simple form.submit()来收集元素并将其作为模型发送到ASP.net MVC控制器,并根据模型上的属性处理验证,并使用回调来处理JSON响应,以仅更新受影响的区域Jquery Ajax在MVC AsP.net中发布表单(验证)?,jquery,ajax,validation,asp.net-mvc-5,asp.net-mvc-5.1,Jquery,Ajax,Validation,Asp.net Mvc 5,Asp.net Mvc 5.1,所以我有一个页面,上面有一些表单,其中一个表单是地址表单,它有三个功能。另存为新建、更新和删除。我想使用form.submit()在客户端和服务器端启动MVC验证,但我不想刷新整个页面 这就是我想要使用simple form.submit()来收集元素并将其作为模型发送到ASP.net MVC控制器,并根据模型上的属性处理验证,并使用回调来处理JSON响应,以仅更新受影响的区域 我知道我可以使用jQuery创建AJAX post请求,但我必须处理所有事情,例如绑定到viewmodel、验证和创建
我知道我可以使用jQuery创建AJAX post请求,但我必须处理所有事情,例如绑定到viewmodel、验证和创建请求本身。不管怎样,我是否能充分利用这两种方法 在MVC中,使用DataAnnotations属性验证模型数据后,您已经完成了服务器端验证,然后可以使用
jQuery.validation库
在客户端进行验证
在这里,您可以使用Ajax.BeginForm post,在这里您可以使用Ajax post进行数据注释验证
数据注释属性
public class Movie {
public int ID { get; set; }
[Required]
public string Title { get; set; }
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
[Required]
public string Genre { get; set; }
[Range(1, 100)]
[DataType(DataType.Currency)]
public decimal Price { get; set; }
[StringLength(5)]
public string Rating { get; set; }
}
jQuery.validation库
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
带有验证的表单
@using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "result" }))
{
<fieldset>
<legend>Movie</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ReleaseDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ReleaseDate)
@Html.ValidationMessageFor(model => model.ReleaseDate)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Genre)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Genre)
@Html.ValidationMessageFor(model => model.Genre)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Price)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Price)
@Html.ValidationMessageFor(model => model.Price)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Rating)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Rating)
@Html.ValidationMessageFor(model => model.Rating)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
@使用(Ajax.BeginForm(新的AjaxOptions{UpdateTargetId=“result”}))
{
电影
@LabelFor(model=>model.Title)
@EditorFor(model=>model.Title)
@Html.ValidationMessageFor(model=>model.Title)
@LabelFor(model=>model.ReleaseDate)
@EditorFor(model=>model.ReleaseDate)
@Html.ValidationMessageFor(model=>model.ReleaseDate)
@LabelFor(model=>model.Genre)
@EditorFor(model=>model.Genre)
@Html.ValidationMessageFor(model=>model.Genre)
@LabelFor(model=>model.Price)
@EditorFor(model=>model.Price)
@Html.ValidationMessageFor(model=>model.Price)
@LabelFor(model=>model.Rating)
@EditorFor(model=>model.Rating)
@Html.ValidationMessageFor(model=>model.Rating)
}
资源:也不要忘记在控制器中的Post方法中进行检查
if(ModelState.IsValid)
因为如果在任何浏览器中使用ctr+shift+i,并且您将删除@Html.EditorFor(model=>model.Price)
客户端验证将不会验证此属性,则客户端验证也不会验证隐藏字段