0

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

我的观点:

<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");   
     } 
}
4

1 回答 1

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>
于 2012-07-04T11:36:14.610 回答