سؤال

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

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

هل كانت مفيدة؟

المحلول

I found the Problem:

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

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, CommentDto>(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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top