gpt4 book ai didi

JQuery 不会将任何内容传递给 ASP.NET Controller

转载 作者:行者123 更新时间:2023-12-01 01:35:51 25 4
gpt4 key购买 nike

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

将 Jquery 对话框 slider 值传递给 Controller ​​。

前端:

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

Controller :

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

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

提前致谢

//硼

最佳答案

看来您在 JavaScript 文件中包含了 Razor 代码:@Url.Action("User", "CheckInMood")

由于这是一个 javascript 文件,因此 Razor 将不会用于呈现该值,因此会将其视为文字。

您需要做的是将 URL 从 View 传递到 JavaScript。

关于JQuery 不会将任何内容传递给 ASP.NET Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13850649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com