我有一個關於Ajax表單提交的問題。下面是代碼:asp.net mvc 3從控制器操作返回消息ajax
控制器
[HttpPost, ValidateAntiForgeryToken, ValidateInput(true)]
public ActionResult Contact(ContactForm model)
{
if (!ModelState.IsValid)
{
return new EmptyResult();
}
else
{
string message = model.Name + " " + model.Telephone + " " + model.Email + " " + model.Address + " " + model.City + " " + model.ZipCode;
//send it
return new EmptyResult();
}
}
查看
<script type="text/javascript">
function PostSuccess() {
$('.success-message').html("Thank you for your interested");
}
function PostFailure() {
$('.success-message').html("Something went wrong... Make sure the required fields are filled out and in correct format");
}
</script>
<div class="contact-form">
@using (@Ajax.BeginForm("Contact", "Info", new AjaxOptions { HttpMethod = "Post", OnFailure = "PostFailure", OnSuccess = "PostSuccess" }))
{
<p>
@Html.LabelFor(x => x.Name, "Your name")
@Html.TextBoxFor(x => x.Name, new { @class = "required" })<span class="required">*</span>
</p>
<p>
@Html.LabelFor(x => x.Email, "Your email")
@Html.TextBoxFor(x => x.Email)<span class="required">*</span>
</p>
<p>
@Html.LabelFor(x => x.Telephone, "Your phone")
@Html.TextBoxFor(x => x.Telephone)<span class="required">*</span>
</p>
<p>
@Html.LabelFor(x => x.Address, "Your address")
@Html.TextBoxFor(x => x.Address)
</p>
<p>
@Html.LabelFor(x => x.ZipCode, "Your zipcode")
@Html.TextBoxFor(x => x.ZipCode)
</p>
<p>
@Html.LabelFor(x => x.City, "Your city")
@Html.TextBoxFor(x => x.City)
</p>
@Html.AntiForgeryToken()
<button type="submit" id="bContact">Send Message</button>
}
<div class="required">* Required Fields</div>
<div class="success-message">Output</div>
我要的是 「成功的消息」 類中的相應的消息,但POST始終是成功的所以我總是得到「感謝你的興趣。」
謝謝。