【发布时间】: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 > 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