我在asp.net部分视图中执行jQuery $ .ajax函数,以调用控制器操作方法来设置会话变量。
我将创建的var对象字符串化。
如果我使用:
contentType: "application/json",
它没有进入控制器的动作方法,但是
success: function
返回警报。
我的控制台日志显示对象属性具有值。
为什么它不进入控制器动作方法?
在这种情况下,如果我使用:
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
它确实进入了控制器的动作方法,但是它收到的参数为NULL
但是我的控制台日志显示对象属性具有值。还有
success: function
返回警报。
为什么输入参数为空?
局部视图:
@model GbngWebClient.Models.LikeOrDislikeVM
<style>
.fa {
cursor: pointer;
user-select: none;
}
.fa:hover {
color: blue;
}
/* I added. */
.my-size {
font-size: 20px;
}
.my-button {
border: none;
padding: 8px 10px;
/* To make the buttons stay on the same line. */
float: left;
font-size: 16px;
margin: 4px 2px;
background-color: white;
}
</style>
<div class="row">
<div>
<button class="blogLike my-button fa fa-thumbs-up"><span class="my-size, likeCount"> : @Model.LikeCount </span></button>
<button class="blogDisLike my-button fa fa-thumbs-down"><span class="my-size, dislikeCount"> : @Model.DisLikeCount</span></button>
</div>
</div>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@Styles.Render("~/Content/css")
<script type="text/javascript">
$(document).ready(function () {
// For testing:
console.log('Here at ready. ');
// JavaScript needs it as 'false'. So using these 'const' to convert them.
const False = false, True = true;
// Set the disabled attributes and color.
SetLike(@Model.LikeDisabled);
SetDisLike(@Model.DisLikeDisabled);
// Create the variable in the image of the LikeOrDislikeVM view model.
var holdLikeOrDislikeVM = {
BlogId : @Model.BlogId,
UserId: @Model.UserId,
LikeCount: @Model.LikeCount,
DisLikeCount: @Model.DisLikeCount,
LikeDisabled: @Model.LikeDisabled,
DisLikeDisabled: @Model.DisLikeDisabled,
};
console.log("hold: " + holdLikeOrDislikeVM.BlogId);
console.log("hold: " + holdLikeOrDislikeVM.UserId);
console.log("hold: " + holdLikeOrDislikeVM.LikeCount);
console.log("hold: " + holdLikeOrDislikeVM.DisLikeCount);
console.log("hold: " + holdLikeOrDislikeVM.LikeDisabled);
console.log("hold: " + holdLikeOrDislikeVM.DisLikeDisabled);
$.ajax({
type: 'POST',
url: '@Url.Action("SetModelSessionVar", "BlogPublished")',
data: { likeOrDislikeVM: JSON.stringify(holdLikeOrDislikeVM) },
//contentType: "application/x-www-form-urlencoded; charset=UTF-8",
contentType: "application/json",
success: function (response) {
alert("Success. Response: " + response);
},
error: function (xhr, ajaxOptions, thrownError) {
alert("Critical Error: something is wrong in the call to SetModelSessionVar! Status: " + xhr.status + ". Error: " + thrownError.toString() + ". Response Text: " + xhr.responseText);
}
})
//--------------------------------------------------------------------------------------
// Set the disabled attribute to true or false.
//--------------------------------------------------------------------------------------
function SetLike(disabledSwitch) {
// For testing:
console.log('Here at Setlike. ' + disabledSwitch);
$(".blogLike").attr('disabled', disabledSwitch);
if (disabledSwitch == true )
{
// Show by color that it was previously liked.
$(".blogLike").css('color', 'green');
}
if (disabledSwitch == false)
{
// Show by color that it can be clicked.
$(".blogLike").css('color', 'black');
}
}
//--------------------------------------------------------------------------------------
// Set the disabled attribute to true or false.
//--------------------------------------------------------------------------------------
function SetDisLike(disabledSwitch) {
// For testing:
console.log('Here at SetDisLike. ' + disabledSwitch);
$(".blogDisLike").attr('disabled', disabledSwitch);
if (disabledSwitch == true)
{
// Show by color that it was previously disliked.
$(".blogDisLike").css('color', 'green');
}
if (disabledSwitch == false)
{
// Show by color that it can be clicked.
$(".blogDisLike").css('color', 'black');
}
}
});
</script>
视图模型:
namespace GbngWebClient.Models
{
public class LikeOrDislikeVM
{
public int BlogId { get; set; }
public int UserId { get; set; }
public int LikeCount { get; set; }
public int DisLikeCount { get; set; }
public bool LikeDisabled { get; set; }
public bool DisLikeDisabled { get; set; }
}
}
控制器动作方法:
[HttpPost]
public void SetModelSessionVar(LikeOrDislikeVM likeOrDislikeVM)
{
// Sets a model session variable according to the argument passed in.
Session["likeOrDislikeVM"] = likeOrDislikeVM;
}
与自由人-m的讨论中,我发现:
对于以下控制器动作方法:
[HttpPost]
public void SetModelSessionVar(string likeOrDislikeVM)
{
Session["likeOrDislikeVM"] = likeOrDislikeVM;
}
这是唯一起作用的1(进入控制器方法的方式为:{“ BlogId”:40,“ UserId”:3,“ LikeCount”:0,“ DisLikeCount”:1,“ LikeDisabled”:false,“ DisLikeDisabled“:true}):
$.ajax({
type: 'POST',
url: '@Url.Action("SetModelSessionVar", "BlogPublished")',
data: { likeOrDislikeVM: JSON.stringify(holdLikeOrDislikeVM) },
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
error: function (xhr, ajaxOptions, thrownError) {
alert("Critical Error: something");
}
})
不起作用(作为空方法进入控制器方法):
$.ajax({
type: 'POST',
url: '@Url.Action("SetModelSessionVar", "BlogPublished")',
data: JSON.stringify(holdLikeOrDislikeVM),
contentType: "application/json",
error: function (xhr, ajaxOptions, thrownError) {
alert("Critical Error: something");
}
})
不起作用(作为空方法进入控制器方法):
$.ajax({
type: 'POST',
url: '@Url.Action("SetModelSessionVar", "BlogPublished")',
data: JSON.stringify(holdLikeOrDislikeVM),
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
error: function (xhr, ajaxOptions, thrownError) {
alert("Critical Error: something");
}
})
不起作用(不进入控制器方法,并且在控制台中没有显示):
$.ajax({
type: 'POST',
url: '@Url.Action("SetModelSessionVar", "BlogPublished")',
data: { likeOrDislikeVM: JSON.stringify(holdLikeOrDislikeVM)},
contentType: "application/json",
error: function (xhr, ajaxOptions, thrownError) {
alert("Critical Error: something");
}
})
现在使用以下控制器动作方法:
[HttpPost]
public void SetModelSessionVar(LikeOrDislikeVM likeOrDislikeVM)
{
Session["likeOrDislikeVM"] = likeOrDislikeVM;
}
这是唯一起作用的1(作为控制器方法使用:{模型的图像-这是我想要的,而不是上面的字符串):
$.ajax({
type: 'POST',
url: '@Url.Action("SetModelSessionVar", "BlogPublished")',
data: { likeOrDislikeVM: holdLikeOrDislikeVM },
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
error: function (xhr, ajaxOptions, thrownError) {
alert("Critical Error: something");
}
})
注意:我不得不从在局部视图中定义的视图模型中创建一个JavaScript对象,因为看来我无法将该视图模型传递给controller方法。不知道为什么。我不明白为什么我不能使用视图模型。
使用此局部视图中定义的视图模型传递给controller方法: 不起作用(进入控制器方法为:null):
$.ajax({
type: 'POST',
url: '@Url.Action("SetModelSessionVar", "BlogPublished")',
data: { likeOrDislikeVM: '@Model' },
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
error: function (xhr, ajaxOptions, thrownError) {
alert("Critical Error: something");
}
})
使用此局部视图中定义的视图模型传递给controller方法: 不起作用(不进入控制器,控制台显示:未定义GbngWebClient ReferenceError:未定义GbngWebClient):
$.ajax({
type: 'POST',
url: '@Url.Action("SetModelSessionVar", "BlogPublished")',
data: { likeOrDislikeVM: @Model},
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
error: function (xhr, ajaxOptions, thrownError) {
alert("Critical Error: something");
}
})