2012-08-08 34 views
0

所以我不知道這裏發生了什麼。我有這種模式彈出窗口,從一個動作加載部分視圖(這可能是問題??)。部分視圖加載完美並且流程正常,但是一旦帖子結束並且應該返回一個json,而不是僅僅關閉然後發佈結果的模式,它會將我重定向到另一個顯示json結果(而不是局部視圖)的頁面。我不完全確定我接近這個方式是否正確。我只需要在動作成功處理後關閉對話框,然後在保存事務或拋出錯誤時返回消息。mvc c#與jquery getjson不關閉模式彈出窗口,但重定向到與json結果的另一頁

任何意見表示讚賞。謝謝!!!該大幹快上彈出

<% using (Html.BeginForm("New", "Registration", FormMethod.Post, new { id = "new-registration" })){ %> 
    <h2>Register Participant:</h2> 
    <div class=""> 
     <%: Html.ValidationSummary(null, new { @class = "" })%> 
     <div class=""> 
      <div class="">Email Address:</div> 
      <div class=""> 
       <%: Html.TextBox("Email", null, new { @class = "" })%> 
      </div> 
     </div> 
     <button type="submit" value="Submit">Register</button> 
    </div> 
<% } %> 

行動

public ActionResult New(){ 
      return PartialView(context.Contest.FirstOrDefault(e => e.Id == 1)); 
     } 

[HttpPost] 
public ActionResult New(FormCollection formCollection) 
     { 
      string email = formCollection["Email"].ToString(); 

      try 
      { 
       if (email == "") 
        throw new Exception("Please provide an email address."); 

       Registration registration = new Registration 
       { 
        ContestId = 1, 
        Email = email 
       }; 

       context.Registration.Add(registration); 
       context.SaveChanges(); 
       return Json(new { success = "true", message = "User succesfully registered to Contest." }); 
      } 
      catch (Exception ex) 
      { 
       //throw ex; 
       return Json(new { success = "false", message = ex.Message }); 
      } 

     } 
+0

你可能會想檢查[這](http://yassershaikh.com/how-to-create-a-modal-popup-in-asp-net-mvc-3-using-jquery/) – Yasser 2012-08-08 05:22:05

回答

0

您需要防止鏈路的默認操作(打開一個新窗口)。新增加載

<script type="text/javascript"> 
    $(function() { 
     $('#modal-link').click(function() { 
      var href = this.href; 
      $('#load-modal-dialog').dialog({ 
       modal: true, 
       open: function (event, ui) { 
        $(this).load(href, function (result) { 
         $('#new-registration').submit(function() { 
          $.getJSON(href, function (data) { 
           if (data.success == true) { 
            $('#messages').html('woo!'); 
           } else { 
            $('#messages').html('dafuq'); 
           } 
           this.dialog('close'); 
          }); 
         }); 
        }); 
       } 
      }); 
      return false; 
     }); 
    }); 
</script> 

我的部分觀點這些行代碼,

$('#modal-link').click(function (event) { 
      //....All content.. 

      event.preventDefault();// this will stop the opening new page 
      event.stopPropergation(); 
      return false; 
    }); 
+0

我試過了,仍然送我到json頁面。:( – gdubs 2012-08-08 05:37:25

+0

試着把它放在'submit'函數中。 – 2012-08-08 05:40:43

+0

這樣做,仍然是一樣的。還包括提交功能中的「事件」。我試着把它放在getJson裏面。 – gdubs 2012-08-08 05:49:50

相關問題