asp.net mvc 3从控制器动作ajax返回一条消息

时间:2011-12-20 01:43:30

标签: 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总是成功的,所以我总是得到“感谢您的兴趣。”

谢谢。

3 个答案:

答案 0 :(得分:2)

如果我理解正确,您希望对您的表单进行验证。也就是说,如果用户输入了一些无效输入,您希望显示PostFailure中的消息,并强制用户在运行控制器的else语句中的代码之前进行更正。您尚未发布模型,因此我不知道您是否正在执行此操作,但要验证表单您应使用Data Annotations

例如,您的模型应包含:

[Required(ErrorMessage = "Name is required!")]
public string Name { get; set; }

表示名称字段。您可以将消息更改为您想要的任何内容,如果您愿意,可以使用特定于Name的消息而不是您拥有的消息。

然后在视图中,您应该包括:

@Html.ValidationMessageFor(x => x.Name)

一定要包括:

<appSettings>
    <add key="ClientValidationEnabled" value="true"/> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/> 
</appSettings>
在您的web.config文件中

启用客户端验证(假设您已包含jQuery验证库,但默认情况下它们包含在ASP.NET MVC3项目中)。

答案 1 :(得分:2)

要在“success-message”类中获取相应的消息,您必须更改操作中的代码。 如:

[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);
    }

答案 2 :(得分:0)

OnSuccessOnFailure表示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函数。