我的表單代碼在我正努力:我在這裏運用模型驗證,我想停下來後我的形式,但它不工作
@using (Html.BeginForm("ContactUs", "Home", FormMethod.Post, new { id = "frmContactUs", enctype = "multipart/form-data" }))
{
<div class="form_style">
@Html.TextBoxFor(m => m.Name, new { @placeholder = "Name", @class = "inputType" })
@Html.ValidationMessageFor(m => m.Name)
</div>
<div class="form_style">
@Html.TextBoxFor(m => m.Email, new { @placeholder = "Email", @class = "inputType" })
@Html.ValidationMessageFor(m => m.Email)
</div>
<div class="form_style">
@Html.TextAreaFor(m => m.Message, new { @placeholder = "Message", @class = "inputType texArea", @style = "resize: none" })
@Html.ValidationMessageFor(m => m.Message)
</div>
<div class="form_style">
<input type="submit" class="inputType Sub_btn" id="btnContactUs" value="SUBMIT" />
</div>
<input type="hidden" id="hdnContactUs" value="@Url.Action("ContactUs", "Home")" />
}
jQuery的AJAX調用腳本:
var url = $('#hdnContactUs').val();
var Name = $('#Name').val();
var Email = $('#Email').val();
var Message = $('#Message').val();
$("frmContactUs").submit(function (e) {
e.preventDefault(); //prevent default form submit
$.ajax({
url: url,
data: { name: Name, email: Email, message: Message },
success: function (data) {
showMsg(data);
return false;
},
cache: false
});
return false;
});
這裏我想模型驗證,但我不希望我的形式,目前它的後張貼到我控制器jsonresult:
public JsonResult ContactUs(string Name, string Email, string Message)
{
JsonResult result = new JsonResult();
return Json(result, JsonRequestBehavior.AllowGet);
}
很難理解你在這裏做什麼。腳本的目的是什麼 - 它只會發佈控件的初始值(而不是編輯值)。因爲你已經爲所有的屬性包含了@ Html.ValidationMessageFor(),所以如果屬性無效並且你已經包含了相關的腳本(jquery.validate和jquery.validate.unobtrusive) – 2015-04-01 23:11:14