我正在尝试制作一个视图,其中用户可以选择曼尼小时(游戏应用程序)日想要玩的时间以及从哪个小时开始并且以间隔为单位。但我正在努力从视图中执行控制器动作方法。 怎么了,因为如果我点击按钮没有任何反应
感谢
我的观点:
<script type="text/javascript">
$('#genhours').click(function () {
var selectedId=$('#dayID').val();
var count = $('#count').val();
var starttime = $('#starttime').val();
var interval = $('#interval').val();
var dayId=
$.ajax({
url: "../Day/MakeHours",
data: { 'dayId': selectedId, 'count': count,'starttime':starttime, 'interval':interval },
type: "post",
cache: false,
},
error: function (xhr, ajaxOptions, thrownError) {
$('#lblError').text("Error encountered while saving the comments.");
}
});
});
</script>
表格:
<div id="dayID" >
@Html.HiddenFor(model => model.dayID)
</div>
<div class="display-label">
Hour per Day
</div>
<div class="display-field">
<input id="count" />
</div>
<div class="display-label">
StartTime
</div>
<div class="display-field">
<input id="starttime" />
</div>
<div class="display-label">
Interval
</div>
<div class="display-field">
<input id="interval"/>
</div>
<br />
</div>
<p>
<input type="submit" id="genhours" value="Generate Hours" />
<input type="button" id ="cancel" title="cancel" onclick="location.href='@Url.Action("Index")'" />
</p>
我的控制器功能:
[HttpPost]
public ActionResult MakeHours(int dayId, int count, string starttime, int interval)
{
for (int i = 0; i < count; i++)
{
Hour hour = new Hour();
hour.day = db.Days.Find(dayId);
if( i==0)
hour.time = starttime;
else
hour.time = starttime+interval;
hour.booked = false;
db.Hours.Add(hour);
db.SaveChanges();
}
return RedirectToAction("Hour/Index");
}
}
答案 0 :(得分:0)
我知道这有点旧......
javascript中有几个语法错误 - 请参阅我为使其工作所做的更改。我已经对参数进行了硬编码,但它应该有所帮助。
<script type="text/javascript">
$(document).ready(function () {
$('#genhours').click(function () {
var selectedId = 1;
var count = 2;
var starttime = 3;
var interval = 4;
var dayId = 5;
$.ajax({
url: "../Test/MakeHours",
data: { 'dayId': selectedId, 'count': count, 'starttime': starttime, 'interval': interval },
type: "post",
cache: false,
error: function (xhr, ajaxOptions, thrownError) {
$('#lblError').text("Error encountered while saving the comments.");
}
});
});
});
</script>