【发布时间】:2014-01-17 16:26:44
【问题描述】:
我已使用以下方式更改了获取提交:
<a style="text-decoration:none;" href="@Url.Action(item.ListAction, item.ListController, new { ids = string.Join("-", item.Ids), categoryId = item.Id, search = (string)ViewBag.Search, location = (string)ViewBag.Location })">
收件人:
@using(Html.BeginForm(null, null, FormMethod.Post, new { id = "homeCategoryForm" }))
{
@Html.AntiForgeryToken()
@Html.Hidden("ids")
@Html.Hidden("categoryId")
@Html.Hidden("search")
@Html.Hidden("location")
}
用 JQuery 提交:
$(document).on("click", ".innerelement", function (e)
{
var elementId = e.target.id.split('_')[1];
action = "/" + $("#controller_" + elementId).val() + "/" + $("#action_" + elementId).val();
$("#homeCategoryForm").attr("action", action);
$("#ids").val($("#ids_" + elementId).val());
$("#categoryId").val($("#categoryId_" + elementId).val());
$("#search").val($("#search_" + elementId).val());
$("#location").val($("#location_" + elementId).val());
$("#homeCategoryForm").submit();
});
控制器:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public virtual ActionResult GetAllByIds(string ids, int categoryId, string search, string location)
{
AdGetAllByCategoryListViewModel model = new AdGetAllByCategoryListViewModel();
model.Ads = Mapper.Map<IList<AdGetAllByCategoryDto>, IList<AdGetAllByCategoryViewModel>>(_adService.GetAllByIds(ids));
model.Category = Mapper.Map<CategoryDto, CategoryViewModel>(_categoryService.GetById(categoryId));
return View(MVC.Ad.Views.GetAllByCategory, model);
}
问题是使用 Form Post 方法的 View 生成的是 application/json View (Source) 而不是 text/html。
编辑:
视图是从 PartialView 渲染的,所以可能是问题所在?
我已经用 PartialView 进行了测试,并且视图的 HTML 被渲染但不是所有的布局视图。
知道为什么吗?
谢谢
【问题讨论】:
-
查看控制器操作([HttpPost] 操作,如果它以这种方式归因)会很有帮助。
-
这可能与您的观点无关。你的控制器代码是什么?
-
代码中是否还有其他(重载)“GetAllByIds”?
-
感谢您这么快的回答,我已经用控制器的代码更新了问题。
-
您似乎正在使用 View(IView, object) 重载。 MVC.Ad.Views.GetAllByCategory 将负责渲染视图。这段代码很可能负责以 json 格式呈现内容。一个快速的检查方法就是使用 View(object) 重载来确保它按预期工作。
标签: c# jquery asp.net-mvc razor content-type