2012-02-07 54 views
1

我在我的應用程序中使用'Ajax.BeginForm',但出於某種原因,我的表單使用正確的json數據重定向到{controller}/{action},而不是start {OnComplete = 「Add_OnComplete」}:Ajax.BeginForm重定向操作頁面(MVC 4)

查看:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script> 

    @using (Ajax.BeginForm("Create", new AjaxOptions { OnComplete = "Add_OnComplete"})) 
    { 
    <fieldset> 
     <legend>Add</legend> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.FullName) 
     </div> 
     <div class="editor-field"> 
      @Html.TextBoxFor(model => model.FullName) 
      @Html.ValidationMessageFor(model => model.FullName) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Email) 
     </div> 
     <div class="editor-field"> 
      @Html.TextBoxFor(model => model.Email) 
      @Html.ValidationMessageFor(model => model.Email) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.PhoneNumber) 
     </div> 
     <div class="editor-field"> 
      @Html.TextBoxFor(model => model.PhoneNumber) 
      @Html.ValidationMessageFor(model => model.PhoneNumber) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Status) 
     </div> 
     <div class="editor-field"> 
      @Html.TextBoxFor(model => model.Status) 
      @Html.ValidationMessageFor(model => model.Status) 
     </div> 

     <p> 
      <input type="submit" value="Create" /> 
     </p> 
    </fieldset> 
} 


    <script type="text/javascript"> 
     function Add_OnComplete(context) { 

      var JsonAdd = context.get_response().get_object(); 

      if (JsonAdd.Success) { 
       //TODO 
      } 

     } 
    </script> 

控制器

public PartialViewResult Create() 
    { 
     return PartialView(); 
    } 

    [HttpPost] 
    public JsonResult Create(Subscribe model) 
    { 
     if (ModelState.IsValid) 
     { 

      _entity.CreateSubscribe(model, SubscribeStatus.Customer.GetHashCode()); 
      var message = new Message(MailSubject, MailBody, model.Email); 

      message.Send(); 
     } 

     return Json(new 
     { 
      Success = true, 
      Message = "The person has been added!" 
     }); 
    } 

什麼我做錯了?我錯過了什麼?

謝謝。

回答

1

刪除get_response()。get_object()的東西。

使用只是context.Success

+0

嗨,這不就來all.It javascript函數自動重定向到操作頁面,寫JSON結果 - {「成功」:真實的,「消息」:「其人已被添加!「} – user541847 2012-02-08 08:46:05

+2

我在這裏測試了它,並且我能夠重現錯誤。它只在解決