borb
borb

Reputation: 33

JQuery won't pass anything to ASP.NET Controller

I've been using like two days now, searching and trying. But still i'm not able to achieve my goal:

Pass an Jquery dialog slider value to the controller.

Frontend:

@model TheMoodApp.Models.tbl_user
@{
    ViewBag.Title = "ControlPanel";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>@Html.DisplayFor(u=>u.Forename)'s Control Panel</h2>
@* Adding header stuff for control panel *@
@section DialogControlPanel{
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
    <script src="http://code.jquery.com/jquery-1.8.3.js"/>
    <script src="https://raw.github.com/brandonaaron/bgiframe/master/jquery.bgiframe.js"/>
    <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"/>
    <script src="~/Scripts/MoodAppScripts.js"/>
    <link href="~/Content/MoodApp.css" rel="stylesheet" />
}
@*The dialog for set initial mood*@
<div id="dialog-modal" title="Hi @Html.DisplayFor(u=>u.Forename)<br/>How do U feel 2Day?">
    <header id="InitialMoodHeader">
        <p>Set UR Mood 
            <input type="text" id="DisplayMood" readonly="readonly"/>
        </p>
    </header>
    <section id="InitialMoodSlider">
        <div id="MoodSlider"></div>
    </section>
</div>

Javascriptfile:

$(document).ready(

function () {
    $("#dialog-modal").dialog({
        height: 140,
        modal: true,
        buttons: {
            "This is my Mood": function () {
                $.ajax({
                    url: '@Url.Action("User", "CheckInMood")',
                    data: JSON.stringify({ mood: $("#DisplayMood").val() }),
                    type: 'POST',
                    contentType: 'application/json; charset=utf-8',
                });
                $(this).dialog("close");
            }
        }

    });

    $("#MoodSlider").slider({
        //TODO: IMPLEMENT JSON TO GET MOOD; ATM FEATURE IS FIXED.
        range: "max",
        min: 0,//scales minimum
        max: 3,//scales maximum
        value: 2,//default value
        slide: function (event, ui) {
            $("#DisplayMood").val(ui.value);//get value from slider and display it
        } 
    });
    $("#DisplayMood").val(
        $("#MoodSlider").slider("value")
        );//set input value from slider
});

Controller:

[HttpPost]
    public JsonResult CheckInMood(string mood)
    {
        //current user checks in with current mood
        uint i = Convert.ToUInt16(mood);
        return Json(View());
    }

Could someone plz help me, getting this to work, and explain to me what i've done wrong?

Thanks in advance

//borb

Upvotes: 3

Views: 264

Answers (3)

borb
borb

Reputation: 33

@Url.Action is only usable from within the razor file itself then, being it in the script tag and so, and now where else? (Just to clarify)

Upvotes: 0

Andy T
Andy T

Reputation: 9881

It appears you are including Razor code: @Url.Action("User", "CheckInMood") in your JavaScript file.

Since this is a javascript file, Razor will not be used to render that value so it will treat it as a literal.

What you'll want to do is pass in the URL from your View into JavaScript.

Upvotes: 1

Jeroen
Jeroen

Reputation: 4023

change:

data: JSON.stringify({ mood: $("#DisplayMood").val() })

to:

data: { mood: JSON.stringify($("#DisplayMood").val()) }

You need to stringify the parameter's value, not the parameter name.

Upvotes: 0

Related Questions