html的form提交json,提交表单将返回application / json而不是text / html

I have changed a Get submit using:

To:

@using(Html.BeginForm(null, null, FormMethod.Post, new { id = "homeCategoryForm" }))

{

@Html.AntiForgeryToken()

@Html.Hidden("ids")

@Html.Hidden("categoryId")

@Html.Hidden("search")

@Html.Hidden("location")

}

Submiting it with 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();

});

The controller:

[HttpPost]

[AllowAnonymous]

[ValidateAntiForgeryToken]

public virtual ActionResult GetAllByIds(string ids, int categoryId, string search, string location)

{

AdGetAllByCategoryListViewModel model = new AdGetAllByCategoryListViewModel();

model.Ads = Mapper.Map, IList>(_adService.GetAllByIds(ids));

model.Category = Mapper.Map(_categoryService.GetById(categoryId));

return View(MVC.Ad.Views.GetAllByCategory, model);

}

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

EDIT:

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

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

Any idea why?

Thanks

Talk1:

It would be helpful to see the controller action (the [HttpPost] action, if it's attributed that way).

Talk2:

it probably has nothing to do with your views. what is your controller code?

Talk3:

is there any other(overload) "GetAllByIds" in the code?

Talk4:

Thanks for your so fast answer, I have updated the question with the controller's code.

Talk5:

You seem to be using the View(IView, object) overload. MVC.Ad.Views.GetAllByCategory will be responsible for rending of the View. It's likely that piece of code is responsible for rending the content in json. A quick way to check would just be to use the View(object) overload to make sure that works as intended.

Solutions1

I found the Problem:

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

@{ Html.RenderAction(MVC.Comment.Create()); }

The Controller is:

public virtual PartialViewResult Create()

{

return PartialView();

}

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);

_commentService.Create(comentDto);

commentViewModel.Result = HeelpResources.CommentViewModelResultMsgOk;

return Json(commentViewModel);

}

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.

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.

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.

Talk1:

Doode.. U saved my day.. Thanks a lot. Just wanna add one more thing for other users reading this if you are posting any of your forms make sure you don't have any raw form (with content type JSON) in your submission cycle. For ex. if you have a page with 2 forms 1 form in pop up and other one on page. And if you have content type for pop up form as JSON then your on-page form will return result with content type JSON only.. Thanks again.

Talk2:

Hi @Rushikesh I'm glade that this problem helped you also. It was really complicated to figure out why this was happening. Thanks for your tips also. Regards.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值