3

我已经使用了两天了,搜索和尝试。但我仍然无法实现我的目标:

将 Jquery 对话框滑块值传递给控制器​​。

前端:

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

Javascript文件:

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

控制器:

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

有人可以帮助我,让它发挥作用,并向我解释我做错了什么吗?

提前致谢

//borb

4

3 回答 3

1

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.

于 2012-12-13T00:01:25.557 回答
0

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

于 2012-12-13T00:14:03.567 回答
0

改变:

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

至:

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

您需要字符串化参数的值,而不是参数名称。

于 2012-12-12T23:55:09.977 回答