问题描述
我在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',contentType: "application/x-www-form-urlencoded; charset=UTF-8",thrownError) {
alert("Critical Error: something");
}
})
$.ajax({
type: 'POST',data: JSON.stringify(holdLikeOrdislikeVM),thrownError) {
alert("Critical Error: something");
}
})
$.ajax({
type: 'POST',data: { likeOrdislikeVM: JSON.stringify(holdLikeOrdislikeVM)},thrownError) {
alert("Critical Error: something");
}
})
现在使用以下控制器动作方法:
[HttpPost]
public void SetModelSessionVar(LikeOrdislikeVM likeOrdislikeVM)
{
Session["likeOrdislikeVM"] = likeOrdislikeVM;
}
这是唯一起作用的1(作为控制器方法使用:{模型的图像-这是我想要的,而不是上面的字符串):
$.ajax({
type: 'POST',data: { likeOrdislikeVM: holdLikeOrdislikeVM },thrownError) {
alert("Critical Error: something");
}
})
注意:我不得不从在局部视图中定义的视图模型中创建一个JavaScript对象,因为看来我无法将该视图模型传递给controller方法。不知道为什么。我不明白为什么我不能使用视图模型。
使用此局部视图中定义的视图模型传递给controller方法: 不起作用(进入控制器方法为:null):
$.ajax({
type: 'POST',data: { likeOrdislikeVM: '@Model' },thrownError) {
alert("Critical Error: something");
}
})
使用此局部视图中定义的视图模型传递给controller方法: 不起作用(不进入控制器,控制台显示:未定义GbngWebClient ReferenceError:未定义GbngWebClient):
$.ajax({
type: 'POST',data: { likeOrdislikeVM: @Model},thrownError) {
alert("Critical Error: something");
}
})
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)