返回视图不工作 - ASP.NET MVC
问题描述:
嗯,这听起来很简单,我已经在我的项目中做了十几次,但这次不起作用,我现在不是为什么。我只想调用一个视图,它具有控制器方法的名称。所以,我有一个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>
答
首先显示EditPfEsp视图中创建的<div id='id'></div>
,然后打你的具体的ActionResult为编辑这样的ActionResult返回你partialview(模型),然后用$("id").empty().append(response)
附加您partailveiw 。
答
修改你的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);
}
});
如果您在返回的HTML,那么它需要'数据类型:“HTML”,'(不是JSON),以及响应如果(response.length> 0){''可能没有多大意义) – 2015-02-23 11:50:13
@StephenMuecke但是斯蒂芬,ajax在调用方法之前。并且它可以毫无问题地调用该方法,并且可以正确传递参数。问题出在方法中,不返回任何视图 – danielpm 2015-02-23 11:55:27
'返回视图(pfIE);'返回html,但您指定ajax结果只接受'json' – 2015-02-23 12:09:14