2013-12-23 70 views
0

根據頁面上的交互流程,我在ajax上有幾個變體。但它只是變化的變化。這裏是其中之一:Ajax不允許查看渲染

$('#btn_skickaEnkel').bind('click', function() { 
     $.ajax({ 
      type: 'POST', 
      url: '/Contact/IntresseAnmälan/', 
      dataType: 'json', 
      data: { 
       Namn: $('#namn').val(), 
       Mail: $('#mail').val(), 
       Info: $('#meddelande').val(), 
       Nivå: $('#nivå').find(":selected").text(), 
       IsEnkel: true, 
       Telefon: $('#nr').val(), 
       ID: function() { 
        var url = window.location.pathname; 
        var id = url.substring(url.lastIndexOf('/') + 1); 
        return id; 
       } 
      }, 
      traditional: true 
     }); 
    }); 

在我的控制器中,我無法重定向或返回不同的視圖。此時,來自JSON的數據不再相關,因爲它已保存到數據庫。

我的控制器:

public ActionResult IntresseAnmälan(BokningContainer bokning) 
    { 
     db = new DbContext(); 

     //Saving some data to database(removed) 

     //Just determening the state of container obj. 
     if (bokning.IsEnkel) 
     { 

      //Geting som information from db (removed) 

      //Creating a mail (removed) 
      email.Send(bokning.Namn, bokning.Mail, body); 

     } 
     else 
     { 

     } 

     //db.SaveChanges(); 

     //This part is not working, I think it's beacuase of the Ajax 
     return View("IntresseAnmälan"); 
    } 

的觀點並沒有呈現,我認爲這是關係到阿賈克斯。該視圖根本不會呈現。有沒有辦法強制返回並忽略ajax?正如我所說的,數據不再需要,因爲內容已經保存到數據庫。

回答

0

你不能渲染視圖上Ajax調用,只需你可以使用表單POST方法還是隻是將其重定向到Ajax調用「成功的情況下」,如下所需的操作:

$('#btn_skickaEnkel').bind('click', function() { 
    $.ajax({ 
     type: 'POST', 
     url: '/Contact/IntresseAnmälan/', 
     dataType: 'json', 
     data: { 
      Namn: $('#namn').val(), 
      Mail: $('#mail').val(), 
      Info: $('#meddelande').val(), 
      Nivå: $('#nivå').find(":selected").text(), 
      IsEnkel: true, 
      Telefon: $('#nr').val(), 
      ID: function() { 
       var url = window.location.pathname; 
       var id = url.substring(url.lastIndexOf('/') + 1); 
       return id; 
      } 
     }, 
     traditional: true, 
     success: function(result) { 
      window.location.href = '@Url.Action("action", "Controller")'; 
     } 
    }); 
}); 
+0

我試過你的解決方案,它不能解決我的問題。即使沒有 - >成功:實現我仍然得到控制器。我甚至渲染_Layout.cshtml,但是當我到達_Layout.cshtml中的@RenderBody()部分時,它不會呈現。 –

0

我簡直不敢相信自己的眼睛當我想出這個「Bugg」時。問題在於,我在某個時候將提交更改爲按鈕。所以表單從不提交。那麼,至少我學到了一些關於視圖和Ajax的知識。

對不起,你抽出時間。