Home ASP MVC Areas and JSON POST
Reply: 0

ASP MVC Areas and JSON POST

user2022
1#
user2022 Published in April 22, 2018, 6:50 am

I have a project with areas and would like to post a view model as JSON to a controller method.

This is what I have, with performance being generated in the default area and passed to the view in area SeatSelection:

$("#addToCartButton").click(function () {
    var json = @Html.Raw(Json.Encode(performance));
    $.ajax({
        url: 'https://@(Request.Url.Host)/SeatSelection/Home/AddToCart',
        type: 'POST',
        dataType: 'json',
        data: json,
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            alert(data);
        }
    });
});

And the action method for testing:

[System.Web.Http.Route("SeatSelection_AddToCart")]        
[System.Web.Http.HttpPost]
public JsonResult AddToCart(PerformanceViewModel performance)
{
    return Json(performance.Name);
}

I created the following route:

context.MapRoute(
    "SeatSelection_AddToCart",
    "SeatSelection/Home/AddToCart",
    new { action = "AddToCart", controller = "Home", id = UrlParameter.Optional },
    namespaces: new string[] { "myProject.Areas.SeatSelection.Controllers" }
);

But all I get is a internal server error 500. I also tried to use [FromBody] and setting a breakpoint to the method, but it is not invoked. I can't figure out what's wrong or missing, please help.

UPDATE This is the json / performance:

PerformanceID=00000000-0000-0000-0000-000000000000&Name=Performance+15&StartDate=%2FDate(1360364400000)%2F&EndDate=%2FDate(1500328800000)%2F&LatestDateBookable=%2FDate(1450911600000)%2F&Organizer=Organizer+15&Location=Location+15&Availability=75&IsFull=false&IsBookable=true&HasPrice=true&BookableSeats=11&BookedSeats=94&Description=Description+of+Performance+15&Price=443

I found an error: "invalid json primitive: performanceid"

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.310852 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO