【问题标题】:Return View not returning - ASP.NET MVC返回视图不返回 - ASP.NET MVC
【发布时间】:2015-02-23 11:47:11
【问题描述】:

嗯,这听起来很简单,我已经在我的项目中做了十几次,但这次没有用,我不知道为什么。我只想调用一个视图,它具有控制器方法的名称。因此,我的一个视图中有一个 Ajax POST,如下所示:

$(".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(与其他人相同的目录和相同的控制器。我做错了什么?我无法让它工作,我不知道为什么。

编辑

这是我想从我的控制器显示的 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>

【问题讨论】:

  • 如果你返回的html,那么它需要是dataType: 'html',(不是json),并且响应是html所以if (response.length &gt; 0) {可能没有多大意义)
  • @StephenMuecke 但是斯蒂芬,ajax 是在调用方法之前。它调用该方法没有问题,并且可以很好地传递参数。问题出在方法上,没有返回任何视图
  • return View(pfIE); 返回 html,但您指定 ajax 结果只接受 json
  • 您可以在浏览器的开发者控制台中检查请求的详细信息 - 尤其是响应 - 以查看发生了什么。除了 Muecke 是对的之外,您必须更改您的 ajax 调用以接受 text/html 或将 EditPfEsp 的返回类型更改为 JsonResult,具体取决于您想要实现的目标。
  • @StephenMuecke 将 ajax 数据类型更改为 html。继续相同,调用 EditPfEsp 控制器后,我不会返回到任何视图。将尝试查看开发者控制台

标签: .net asp.net-mvc model-view-controller


【解决方案1】:

首先创建&lt;div id='id'&gt;&lt;/div&gt;,然后点击您的特定actionresult 进行编辑,以便actionresult 返回给您partialview(model),然后在您的parttailveiw 后面附加$("id").empty().append(response)

【讨论】:

  • 这或多或少像我想要的那样工作!这使得视图出现在当前视图中。但我想用我想展示的视图打开一个新窗口!我可以这样做吗?
  • 这对我有用,将我想要的视图插入到我的 atual 视图中。谢谢!
【解决方案2】:

修改您的 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);
    }
    });

【讨论】:

  • 是的! “错误”和“内部服务器错误”。为什么?他们从哪里来?通过我的整个控制器后它给了我错误!
  • 可能会抛出异常,因为例如您传递给 View 错误的模型。
  • @Mariusz 我将正确的模型传递给视图!
  • 当我调试应用程序时,我的控制器接收到的值很好。我将用我想去的地方更新主题
  • @AmanBaloch 在visualstudio中,如果我在脚本中打断它不会命中。但是使用萤火虫我可以看到这与 ajax 回调有关。我添加了“完成:函数(jqXHR,状态){alert(“alert”)},它在回调中触发,但没有像以前那样发生任何事情
猜你喜欢
  • 2011-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-20
相关资源
最近更新 更多