2011-12-20 113 views
1

我有一個關於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始終是成功的所以我總是得到「感謝你的興趣。」

謝謝。

回答

2

要在「成功消息」類中獲得適當的消息,您必須在您的操作中更改代碼。 如:

[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); 
    } 
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函數會解析通話的結果。

2

如果我理解正確,您希望驗證您的表單。也就是說,如果用戶輸入了一些無效的輸入,並且在運行控制器的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項目中)。

相關問題