Form submission returns application / json instead of text / html

I changed the Get clause 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 })"> 

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") } 

Transmission using 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(); }); 

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<AdGetAllByCategoryDto>, IList<AdGetAllByCategoryViewModel>>(_adService.GetAllByIds(ids)); model.Category = Mapper.Map<CategoryDto, CategoryViewModel>(_categoryService.GetById(categoryId)); return View(MVC.Ad.Views.GetAllByCategory, model); } 

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

EDIT:

The view is a render from PartialView, so maybe this is a problem?

I tested with PartialView, and the HTML representation is rendered, but not all kinds of layout.

Any idea why?

thanks

0
source share
1 answer

I found a problem:

In the layout of the view, I have a commentary form:

 <!-- Comments form container --> <div class="comentsform"> <!-- Comments form --> @{ Html.RenderAction(MVC.Comment.Create()); } </div> <!-- Comments form container closed --> 

Controller:

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

The problem here is that I also have a JSON action to post a jQuery comment:

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

Thus, it seems that when the layout is displayed from the action of the POST form, it will look for all the [HttpPost] actions from the Html.RenderAction presented in the layout.

In this case, and since I have an Html.RenderAction with an [HttpPost] action like JsonResult, all the results of the result are converted to a JSON response.

So now all I have to do is change the JSON Action name to the public virtual JsonResult CreateSend, and the problem is solved!

Thanks again for the availability of everyone to help.

+1
source

All Articles