user3562751
user3562751

Reputation: 263

ASP.NET MVC 4: jquery 500 error

I am very new to AJAX.

I am trying to submit two arrays to a controller and then redirect to a new view. I am getting an error in my console: 'POST //.../League/AddPreRankList/ 500 (Internal Server Error)'

Here is my code,

JS

//handler for pre rank list submission
$("#SubmitPreRankList").submit(function (e) {
    e.preventDefault();

    var PlayerIDS = new Array();
    $(".DraftList").each(function () {
        PlayerIDS.push(this.id);

    });

    var PreRanks = new Array();
    $(".DraftList").each(function () {
        PreRanks.push(($(this).index()));
    });

    //submit preranks
    $.ajax({
        url: '/League/AddPreRankList/',
        type: 'POST',
        data: { PlayerIDS: PlayerIDS, PreRanks: PreRanks },
        success: function (response) {
            window.location = response.url;
        }

    });

});

Controller

    [HttpPost]
    public JsonResult AddPreRankList(int [] PlayerIDS, int [] PreRanks)
    {
        //Get the id from url
        string[] uri = Request.Url.Segments;
        int id = Convert.ToInt32(uri[3]);


        return Json(new { url = Url.Action("Home", "League", new { id = id }) });
    }

View

            <form id="SubmitPreRankList">
                <div class="row">
                    <div class="small-9 columns">
                        <h2>My Pre-Rank List</h2>
                    </div>
                    <div class="small-3 columns">
                        <p>
                            <input type="submit" value="Submit" />
                        </p>
                    </div>
                </div>
                <div class="row">
                    <div class="large-12 columns">
                        <ol id="UserPreRankList"></ol>

                    </div>
                </div>
            </form>

Upvotes: 0

Views: 431

Answers (1)

bsb_coffee
bsb_coffee

Reputation: 7061

Try using JSON.stringify()

data: JSON.stringify({ PlayerIDS: PlayerIDS, PreRanks: PreRanks })

also, you might want to use

contentType: "application/json; charset=utf-8"

Upvotes: 1

Related Questions