0
接收到的服務器成功消息下面
代碼準備好測試:MVC3停止頁面一旦從控制器
問題是,當啓用e.preventDefault();而不是由ValidationMessageFor生成的每個控件的前面出現的單個錯誤,並在e.preventDefault()時顯示 。已啓用。所有的錯誤都工作正常,但一旦代碼處理,併成功返回的消息從 服務器端使整個網頁提交/刷新:(
問:我如何阻止整個頁面刷新自/回傳如果E。的preventDefault();是啓用和從服務器獲得成功消息
<script type="text/javascript">
$(function() {
$("#personCreate").click(function (e) {
//e.preventDefault(); -------> Here is the problem
var profile = {
FirstName: $("#FirstName").val(),
LastName: $("#LastName").val()
};
$.ajax({
url: '/Test/save',
type: 'POST',
dataType: 'json',
data: JSON.stringify(profile),
contentType: 'application/json; charset=utf-8',
//beforeSend: function() { $("#saveStatus").html("Saving").show(); },
//complete: function() { $("#saveStatus").html("Saving").hide(); },
success: function (data) {
// get the result and do some magic with it
var message = data.Message;
$("#resultMessage").html(message);
}
});
});
});
</script>
//控制器代碼
[HttpPost]
public ActionResult Save(CreateViewModel userVm)
{
if (ModelState.IsValid)
{
return Json(new CreateViewModel { Message = "Passed" });
}
else
{
return Json(new CreateViewModel { Message = "Failed" });
}
}
?
//Create.cshtml
<span id="saveStatus"></span>
@using(Html.BeginForm())
{
<fieldset>
<legend>Contact Information</legend>
<div>
<table class="form-spacing">
<tr>
<td class="cell-one">* @Html.LabelFor(model => model.FirstName) :</td>
<td class="cell-two">@Html.TextBoxFor(model => model.FirstName, new { @class = "big-field", tabindex = "1" })</td>
<td class="cell-three" >@Html.ValidationMessageFor(model => model.FirstName)</td>
</tr>
<tr>
<td class="cell-one">* @Html.LabelFor(model => model.LastName) :</td>
<td class="cell-two">@Html.TextBoxFor(model => model.LastName, new { @class = "big-field", tabindex = "2" })</td>
<td class="cell-three" >@Html.ValidationMessageFor(model => model.LastName)</td>
</tr>
</table>
</div>
</fieldset>
<p>
<input type="submit" value="Save" id="personCreate" />
</p>
}
<div>
<span id="resultMessage"></span>
</div>
相反的ActionResult的,如果我不是它不工作和頁面回發還是返回JsonResult。 – Pirzada 2010-12-10 06:25:46