2015-02-23 126 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