提交表单是返回application / json而不是text / html
我已使用以下内容更改了Get submit:
至:
@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, IList>(_adService.GetAllByIds(ids)); model.Category = Mapper.Map(_categoryService.GetById(categoryId)); return View(MVC.Ad.Views.GetAllByCategory, model); }
问题是使用Form Post方法的View正在生成一个application / json View(Source)而不是text / html。
编辑:
视图是从PartialView渲染的,所以可能是问题所在?
我已经使用PartialView进行了测试,并且渲染了视图的HTML,但不是所有的布局视图。
知道为什么吗?
谢谢
我发现了问题:
在视图的布局中我有一个camentforms:
@{ Html.RenderAction(MVC.Comment.Create()); }
控制器是:
public virtual PartialViewResult Create() { return PartialView(); }
这里的问题是我还有一个JSON Action来发送jQuery的注释:
[HttpPost] [ValidateAntiForgeryToken] public virtual JsonResult Create(CommentViewModel commentViewModel) { CommentDto comentDto = Mapper.Map(commentViewModel); _commentService.Create(comentDto); commentViewModel.Result = HeelpResources.CommentViewModelResultMsgOk; return Json(commentViewModel); }
所以看来,当布局呈现来自Form POST动作时,它将搜索布局中呈现的Html.RenderAction的所有[HttpPost]动作。
在这种情况下,因为我有一个带有JsonResult类型的[HttpPost] Action的Html.RenderAction,所有结果View都在JSON响应中转换。
所以现在,我唯一要做的就是将JSON Action的名称更改为公共虚拟JsonResult CreateSend,例如,解决问题!
再次感谢所有人的帮助。
- 如何将文本框值传递给jQuery .ajax方法?
- 无法使用Select2以编程方式选择选项
- 需要添加哪些jQuery引用才能使Bootstrap正常工作,在哪里?
- 使用分页时,使用DataTable在MVC中使用实体提交模型失败
- 为什么在asp.net mvc会话超时后jquery ajax调用失败?
- 如何在浏览器页面源中隐藏jquery ajax中的详细信息
- 将带有数据和文件的JSON发布到Web Api – jQuery / MVC
- 应该将带有jquery代码的脚本块放在哪里放在ASP.NET MVC母版页上?
- 当FormsLogin会话不再处于活动状态时,ASP.NET MVC强制将AJAX请求重定向到登录页面