我在模型中有难度访问器:
public int Difficulty
{
get;
set;
}
我的HTML看起来像这样:
<a href="#" onclick="easy()">Easy</a>
<a href="#" onclick="average()">Average</a>
<a href="#" onclick="hard()">Hard</a>
然后我想通过按任意链接来改变难度值。我试着用下面的脚本来做,但它显然不起作用。我该怎么办?
function easy() {
@Model.Difficulty = 15
location.reload();
}
function average() {
@Model.Difficulty = 30
location.reload();
}
function hard() {
@Model.Difficulty = 45
location.reload();
}
答案 0 :(得分:1)
您可以发出ajax请求
function easy()
{
var difficulty = 15;
changeDifficulty();
}
function changeDifficulty()
{
var serviceURL = '/Controller/changeDifficulty';
$.ajax({
url: serviceURL,
data: { d: difficulty },
type: 'POST',
success: function () {
alert("Success");
},
error: function () {
alert("Something went wrong");
}
});
}
在您的控制器中
public void changeDifficulty(int d)
{
//Update your model here
}