返回视图不起作用 - ASP.NET MVC

时间:2022-06-09 08:22:44

Well, this sound simple, and I've done it a dozen times already in my project, but this time isn't working and I don't now why. I just want to call a view, that have the name of the controller method. So, I have an Ajax POST in one of my Views, that is the following:

嗯,这听起来很简单,我已经在我的项目中完成了十几次,但这次没有用,我现在不知道为什么。我只想调用一个具有控制器方法名称的视图。所以,我的一个视图中有一个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");
    }
    }
    });

It calls the method EditPfEsp just fine, wich is this:

它调用方法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);
    }

But then, the method dont return the view EditPfEsp that I have (same directory and same controller as the others. What I am doing wrong? I cant get this working and I dont have idea why.

但是,该方法不会返回我拥有的视图EditPfEsp(与其他目录相同的目录和控制器。我做错了什么?我不能让这个工作,我不明白为什么。

edit

This is the EditPfEsp view that I want to show from my controller

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

2 个解决方案

#1


0  

first create the <div id='id'></div> ,then hit your specific actionresult for edit so actionresult return to you partialview(model) and then append your partailveiw with $("id").empty().append(response).

首先创建

,然后点击你的特定actionresult进行编辑,以便actionresult返回你的partialview(模型),然后用$(“id”)附加你的partailveiw。empty()。append (响应)。

#2


0  

Modify your ajax function to include the error callback function as below:

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

#1


0  

first create the <div id='id'></div> ,then hit your specific actionresult for edit so actionresult return to you partialview(model) and then append your partailveiw with $("id").empty().append(response).

首先创建

,然后点击你的特定actionresult进行编辑,以便actionresult返回你的partialview(模型),然后用$(“id”)附加你的partailveiw。empty()。append (响应)。

#2


0  

Modify your ajax function to include the error callback function as below:

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