提交表单将返回application/json而不是text/html [英] Submiting a Form is returning application/json instead of text/html

查看:130
本文介绍了提交表单将返回application/json而不是text/html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用以下方式更改了获取提交":

I have changed a Get submit using:

<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正在生成应用程序/json视图(源),而不是text/html.

The problem is that the View using the Form Post method is producing a application/json View (Source) and not the text/html.

视图是从PartialView渲染的,所以也许是问题所在?

The view is been rendering from a PartialView, so maybe it's the problem?

我已经使用PartialView进行了测试,并且呈现了View的HTML,但未呈现所有的Layout View.

I have tested with PartialView and the HTML of the View is rendered but not the all Layout View.

知道为什么吗?

谢谢

推荐答案

我发现了问题:

在视图的布局中,我有一个注释形式:

In the Layout of the View I have a coment form:

<!-- Comments form container -->
<div class="comentsform">

    <!-- Comments form -->
    @{ Html.RenderAction(MVC.Comment.Create()); }

</div>
<!-- Comments form container closed -->

控制器为:

public virtual PartialViewResult Create()
{
    return PartialView();
}

这里的问题是我也有一个JSON Action通过jQuery发送评论:

The issue here is that I also have a JSON Action to send the comment by jQuery:

[HttpPost]
[ValidateAntiForgeryToken]
public virtual JsonResult Create(CommentViewModel commentViewModel)
{
    CommentDto comentDto = Mapper.Map<CommentViewModel, CommentDto>(commentViewModel);

    _commentService.Create(comentDto);

    commentViewModel.Result = HeelpResources.CommentViewModelResultMsgOk;

    return Json(commentViewModel);
}

所以看来,当从Form POST动作呈现布局时,它将搜索Html.RenderAction出现在布局中的所有[HttpPost]动作.

So it seems that, when the Layout is rendered coming from a Form POST action, it will search for all the [HttpPost] Actions of the Html.RenderAction presents in the Layout.

在这种情况下,由于我的Html.RenderAction具有类型为JsonResult的[HttpPost] Action,因此所有结果View都将在JSON响应中转换.

In this case, and because I have a Html.RenderAction with an [HttpPost] Action of type JsonResult, the all result View is converted in a JSON response.

所以现在,我唯一要做的就是将JSON操作的名称更改为例如公共虚拟JsonResult CreateSend,并解决问题!

So now, the only thing I have to do is to change the name of the JSON Action to public virtual JsonResult CreateSend for example, and Problem Solved!

再次感谢您提供的所有帮助.

Thanks again for the availability of all to help.

这篇关于提交表单将返回application/json而不是text/html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆