一次按鈕點擊在控制器端提交mvc3 ajax。怎麼樣?
如何處理在服務器端Ajax調用,這樣我DataAnnotation工作 ,我得到成功或錯誤信息,我不明白。
<script src="../../../../Content/Scripts/jquery-1.4.4-vsdoc.js" type="text/javascript"></script
<script type="text/javascript">
$(function()
{
$("#createButton").click(function()
{
var profile = {
FirstName: $("#FirstName").val(),
LastName: $("#LastName").val(),
Email: $("#Email").val()
};
$.ajax({
url: "/Profile/Create",
type: "Post",
data: JSON.stringyfy(profile),
dataType: "json",
contentType: "Application/json; charset=utf-8",
success: function() {
$("#message").html("Profile Saved.");
},
error: function() {
$("#message").html("Error occured");
}
});
return false;
});
});
</script>
//Server side
public ActionResult Create(string confirmButton, CreateViewModel userVm)
{
if (confirmButton != "Create Profile") return RedirectToAction("Index");
if (!ModelState.IsValid)
return View("Create", userVm);
User user = new User();
Mapper.Map(userVm, user);
_repository.Create(user);
return RedirectToAction("Details", new { id = user.UserId });
}
我明白了。但是就像在上面的控制器代碼中,我一旦驗證失敗就返回。而不是返回視圖(「創建」,userVm);我應該返回什麼。那就是我面臨的問題。 – Pirzada 2010-12-09 07:52:43