2015-02-23 124 views
1

嗯,這聽起來很簡單,我已經在我的項目中做了十幾次,但這次不起作用,我現在不是爲什麼。我只想調用一個視圖,它具有控制器方法的名稱。所以,我有一個Ajax POST在我的觀點之一,即是:返回視圖不工作 - ASP.NET MVC

$(".btn-default").on("click", function (event, params) { 
console.log($(".chosen-select").val()); 

$.ajax({ 
    url: '@Url.Action("EditPfEsp", "Lottery")', 
    type: 'GET', 
    dataType: 'html', 
    cache: false, 
    traditional : true, 
    data: { bdoIds: $(".chosen-select").val() }, 
    success: function (response) { 
    if (response.length > 0) { 
    alert(response); 
    } 
    else { 
    alert("response length zero"); 
    } 
    } 
    }); 

它調用方法EditPfEsp就好了,至極的是:

public ActionResult EditPfEsp(string[] bdoIds) 
    { 
     IEnumerable<PF> pF = new List<PF>(); 
     pF = service.GetPF(); 
     List<PF> pFList = new List<PF>(); 
     pFList = pF.ToList(); 
     List<PF> pfListFinal = new List<PF>(); 

     for (int i = 0; i < bdoIds.Count(); i++) 
     { 
      for (int x = 0; x < pFList.Count(); x++) 
      { 
       int id = Convert.ToInt32(bdoIds[i]); 
       int pfunc = Convert.ToInt32(pFList[x].Func); 
       if (id == pfunc && (pFList[x].Tipo_sorteio == "ESPECIAL" || pFList[x].Tipo_sorteio == "especial" || pFList[x].Tipo_sorteio == "Especial")) 
       { 
        pfListFinal.Add(pFList[x]); 
       } 
      } 
     } 

     IEnumerable<PF> pfIE = new List<PF>(); 
     pfIE = pfListFinal.AsEnumerable(); 

     return View(pfIE); 
    } 

但隨後,該方法不要返回我的觀點EditPfEsp(相同的目錄和相同的控制器其他人。我做錯了什麼?我不能得到這個工作和我沒有知道爲什麼。

編輯

這是我想從我的控制器

@model IEnumerable<AldpModel.Entities.PF> 



<h2>Editar ponderações de funcionários selecionados</h2> 

<div class="filterbox"> 
    <table class="table"> 
     <tr> 

      <th> 
       <b>@Html.DisplayNameFor(model => model.Tipo_sorteio)</b> 
      </th> 
      <th> 
       <b>@Html.DisplayNameFor(model => model.Funcionario)</b> 
      </th> 
      <th> 
       <b>@Html.DisplayNameFor(model => model.Nome_Funcionario)</b> 
      </th> 
      <th> 
       <b>@Html.DisplayNameFor(model => model.Ponderacao)</b> 
      </th> 
      <th> 
       <b>@Html.DisplayNameFor(model => model.Incluir_sorteio)</b> 
      </th> 

      <th></th> 
     </tr> 

     @foreach (var item in Model) 
     { 
      <tr> 

       <td> 
        @Html.DisplayFor(modelItem => item.Tipo_sorteio) 
       </td> 
       <td> 
         @Html.DisplayFor(modelItem => item.Funcionario) 
       </td> 
       <td> 
        @Html.DisplayFor(modelItem => item.Nome_Funcionario) 
       </td> 
       <td> 
         @Html.DisplayFor(modelItem => item.Ponderacao) 
       </td> 
       <td> 
        @Html.DisplayFor(modelItem => item.Incluir_sorteio) 
       </td> 

       <td> 
        @Html.ActionLink("Editar", "Edit", new { id = item.Funcionario }) | 
        @Html.ActionLink("Detalhes", "Details", new { id = item.Id }) | 
        @Html.ActionLink("Apagar", "Delete", new { id = item.Id }) 
       </td> 
      </tr> 
     } 

    </table> 
</div> 
+4

如果您在返回的HTML,那麼它需要'數據類型:「HTML」,'(不是JSON),以及響應如果(response.length> 0){''可能沒有多大意義) – 2015-02-23 11:50:13

+0

@StephenMuecke但是斯蒂芬,ajax在調用方法之前。並且它可以毫無問題地調用該方法,並且可以正確傳遞參數。問題出在方法中,不返回任何視圖 – danielpm 2015-02-23 11:55:27

+0

'返回視圖(pfIE);'返回html,但您指定ajax結果只接受'json' – 2015-02-23 12:09:14

回答

0

首先顯示EditPfEsp視圖中創建的<div id='id'></div>,然後打你的具體的ActionResult爲編輯這樣的ActionResult返回你partialview(模型),然後用$("id").empty().append(response)附加您partailveiw 。

+0

這或多或少像我想要的!這使得視圖出現在當前視圖中。但我想打開一個新窗口,以期展示我的觀點!我可以這樣做嗎? – danielpm 2015-02-24 17:42:37

+0

這對我來說很有效,插入了我想要的視圖。謝謝! – danielpm 2015-02-25 11:38:50

+0

您的歡迎。#danieplm – 2015-02-25 12:08:21

0

修改你的Ajax功能,包括如下錯誤回調函數:

$.ajax({ 
    url: '@Url.Action("EditPfEsp", "Lottery")', 
    type: 'GET', 
    dataType: 'html', 
    cache: false, 
    traditional : true, 
    data: { bdoIds: $(".chosen-select").val() }, 
    success: function (response) { 
     if (response.length > 0) { 
     alert(response); 
     } 
     else { 
     alert("response length zero"); 
     } 
    } 
    error: function(jqXHR, textStatus, errorThrown) 
    { 
     alert(textStatus); 
     alert(errorThrown); 
    } 
    }); 
+0

添加看看它是否提醒你有任何錯誤? – 2015-02-23 14:43:14

+0

是的! 「錯誤」和「內部服務器錯誤」。爲什麼?他們從哪裏來?它通過我的整個控制器後給我錯誤! – danielpm 2015-02-23 15:13:20

+0

可能會拋出異常,因爲例如您傳遞給查看錯誤的模型。 – Mariusz 2015-02-23 15:42:58