我有我的應用程序的部分視圖我正在部分視圖(不使用任何表單標籤)創建用戶註冊功能,我打電話給控制器的操作方法使用ajax-jquery &傳遞用戶詳細信息(模型)的行動方法,在調用ajax方法之前,我需要在客戶端驗證用戶詳細信息。我怎樣才能做到這一點?ajax方法調用之前驗證模型數據
需要在ajax調用之前驗證模型(不使用任何表單標籤)。
@model MVC4Demo.Models.Student
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script type="text/javascript">
function onClientClick() {
var obj = { FirstName: "name", City: "abc" };
$.ajax({
url: "/Home/AjaxDemo",
data: JSON.stringify(obj),
type: "POST",
datatype: "json",
contentType: "application/json; charset=utf-8",
success: function (result) {
alert('success');
},
error: function() {
alert("error");
}
});
}
</script>
<div id="Main">
<label>Student</label>
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.City)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.City)
@Html.ValidationMessageFor(model => model.City)
</div>
<p>
<input type="submit" onclick="onClientClick()" value="Create" />
</p>
</div>
<div>
@Html.ActionLink("Back to List", "Index")
</div>
爲什麼你不想把
@migontech我在部分視圖上實現註冊功能&我已經在父視圖上有表單標籤,在註冊時我需要驗證僅部分視圖的值不是整體形式 – nirav 2013-03-27 05:23:13