asp.net mvc 3从控制器操作返回消息ajax
问题描述:
我有一个关于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始终是成功的所以我总是得到“感谢你的兴趣。”
谢谢。
答
要在“成功消息”类中获得适当的消息,您必须在您的操作中更改代码。 如:
[HttpPost, ValidateAntiForgeryToken, ValidateInput(true)]
public ActionResult Contact(ContactForm model)
{
if (!ModelState.IsValid)
{
return Json("", JsonRequestBehavior.AllowGet);
}
else
{
string message = model.Name + " " + model.Telephone + " " + model.Email + " " + model.Address + " " + model.City + " " + model.ZipCode;
return Json(message, JsonRequestBehavior.AllowGet);
}
}
并且在成功函数中,你把参数放在那里。
function PostSuccess(data) {
$('.success-message').html(data);
}
答
OnSuccess
和OnFailure
指的是AJAX调用是否成功或失败。从MSDN为OnFailure:
This function is called if the response status is not in the 200 range.
您可以执行以下操作来获得onFailure处运行:
throw new HttpException(404, "Not Found");
但我会建议,这将是更好的改变成功事件调用JavaScript函数会解析通话的结果。
答
如果我理解正确,您希望验证您的表单。也就是说,如果用户输入了一些无效的输入,并且在运行控制器的else
语句中的代码之前强制用户进行更正,则需要显示PostFailure
中的消息。你还没有发布你的模型,所以我不知道你是否正在这样做,但对于表单来验证你应该使用Data Annotations。
例如,你的模型应该包含:
[Required(ErrorMessage = "Name is required!")]
public string Name { get; set; }
为name字段。您可以将消息更改为任何想要的内容,如果您愿意,可以使用特定于Name
的消息,而不是通用消息。
然后在视图中,您应该包括:
@Html.ValidationMessageFor(x => x.Name)
而且一定要包括:
<appSettings>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>
在web.config
文件中启用客户端验证(假设你已经包括jQuery验证库,但它们默认包含在ASP.NET MVC3项目中)。