我正在尝试创建一个视图,用户可以在其中选择一天要玩多少小时(游戏应用程序)以及从哪个小时开始以及以哪个间隔开始。但我正在努力从视图中执行控制器操作方法。出了什么问题,因为如果我点击按钮什么也没有发生
我的观点:
<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");
}
}