可以'在使用Javascript/AjaxMVC之后,不会返回到任何视图

Can't return to any view after using Javascript/Ajax MVC

本文关键字:之后 返回 视图 任何 AjaxMVC Javascript 可以      更新时间:2024-06-19

我有一个问题,我不知道如何解决。我做了大量的研究,找不到类似的东西,甚至我不知道如何解决或为什么会发生这种情况(可能是因为我是mvc的新手)。

所以,我有一个方法

public ActionResult EscolherFuncionarios()
        {
            SorteioEspecial sorteioEspecial = new SorteioEspecial();
            List<Funcionario> list = new List<Funcionario>();
            list = sorteioEspecial.GetFuncionarios().ToList().OrderBy(x => x.Nome).ToList();
            ViewBag.FuncionarioId = new SelectList(list, "Id", "Nome");
            return View(sorteioEspecial);
        }  

谁称之为视图

@model ApdlSorteio.Models.SorteioEspecial
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
    <div class="col-md-10" style="float:left">
        @Html.DropDownList("FuncionarioId", null, htmlAttributes: new { @class = "chosen-select", @data_placeholder = "Escolha os funcionários para sorteio", @multiple = "true" })
    </div>
    <div class="form-group" style="float:left">
        <div class="col-md-10" style="float:left">
            <input type="submit" value="Adicionar" class="btn-default" />
        </div>
    </div>
    <br />
    <br />
    <br />
    <br />
    <br />
    <div id="MyDiv"></div>
    <div class="form-group" style="float:left">
        <div class="col-md-10" style="float:left">
            <input type="submit" value="Editar ponderações" class="breadcrumb" />
        </div>
    </div>
    <br />
    <br />
    <br />


<script src="~/Scripts/jquery-2.1.1.js"></script>
<script src="~/Scripts/jquery.js"></script>
<script src="~/Scripts/chosen.proto.js"></script>
<link href="~/Scripts/chosen.css" rel="stylesheet" />
<script src="~/Scripts/chosen.jquery.js"></script>

<script>
    $(".chosen-select").chosen({
        disable_search_threshold: 10,
        no_results_text: "Nenhum funcionário encontrado!",
        width: "95%"
    });
</script>
<script>
    $(document).ready(function () {
     $(".breadcrumb").on("click", function (event, params) {
        var selectedValues = [];
        $(".chosen-select :selected").each(function () {
            selectedValues.push($(this).attr('value'));
        }); 
        $.post('@Url.Action("EditarPonderacoesEspecialSecond","Sorteios")', { bdoIds: selectedValues });
        });
    });
</script>

在脚本中,我有一个按钮,它将调用方法EditarPonderacoesEspecialSecond,其代码为

public ActionResult EditarPonderacoesEspecialSecond(string[] bdoIds)
        {
            List<PonderacaoFuncionario> Getpf = new List<PonderacaoFuncionario>();
            Getpf = service.GetPonderacaoFuncionario().ToList();
            List<PonderacaoFuncionario> pfList = new List<PonderacaoFuncionario>();
            for (int x = 0; x < bdoIds.Count(); x++)
            {
                for (int y = 0; y < Getpf.Count(); y++)
                {
                    if (Convert.ToInt32(bdoIds[x]) == Getpf[y].Funcionario)
                    {
                        pfList.Add(Getpf[y]);
                    }
                }
            }
            //return RedirectToAction("Details", new { id = 90 });
            return View(pfList);
        }

将称之为视图

@model IEnumerable<ApdlModel.Entities.PonderacaoFuncionario>
<div class="filterbox">
    <table class="table">
        <tr>
            <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>
        <tr>
            @foreach (var item in Model)
            {
                <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>
            }
        </tr>
</table>
</div>

问题是这种观点没有体现出来。一开始我认为这在视图上会有问题,所以我使用了RedirectToAction(就像你可以在控制器上看到的评论一样),但这也没有显示任何视图,但如果我通过另一个页面转到方法"Details",它就可以工作了。我现在真的不知道发生了什么,需要一些帮助这里

当您使用$.post时,它会调用您的操作,然后返回一堆HTML。然后你什么也不做,所以浏览器就把它扔掉了。如果您只想转到新页面,请定期发布,不要使用ajax。Ajax更适合于如果您需要调用服务器来获取一些信息来更新当前页面,而不是转到新页面。

编辑

要进行常规发布,请将所有内容包装在一个表单中,将操作设置为发布,然后将提交按钮放在其中的某个位置。另外,去掉javascript帖子。若要创建表单,请使用Html查看。BeginForm。

每当您在不指定视图名称的情况下调用View()方法时,您的代码都会在AreaRegistration中为其配置路由的文件夹中搜索与该方法同名的视图。

请确保您的视图位于正确的文件夹中,并且如果您要调用没有字符串名称的view()方法,则它与调用它的方法具有相同的名称。

如果你说调用"详细信息"有效,请将你想要的视图放在与详细信息视图相同的文件夹中(如果不是),或者更改路线的配置以匹配视图。

您没有对AJAX调用的返回值执行任何操作。与传统上通过地址栏加载URL不同,当你进行AJAX调用时,浏览器不会自己做任何事情。你有责任用回应来做一些事情。在本例中,将返回的HTML添加到DOM中。这是通过回调完成的。

$.post('@Url.Action("EditarPonderacoesEspecialSecond","Sorteios")', { bdoIds: selectedValues }, function (result) {
    $('#SomeElement').html(result);
});

尝试在中指定Viewpath"return View(obj)"

"return View(@"~''Views''Shared''Home",obj)"