我希望能够单击按钮并将对象添加到模型中的列表中。它应该在每次单击按钮时添加一个新对象。我还有一个部分视图,根据此列表中的对象数量进行渲染,当发生这种情况时也需要更新。我知道我需要使用ajax,但这不是我非常熟悉的东西。
这是控制器:
public ActionResult NewScheduleRequest()
{
var srm = new NewScheduleRequestModel();
srm.Request = new ResourceRequest();
srm.Request.ResourceRequestScheds.Add(new ResourceRequestSched(){StartDateRequested = DateTime.Today, EndDateRequested = DateTime.Today.AddDays(7)});
return PartialView("NewScheduleRequestView", srm);
}
问题的html:
<div id="timeFrames">
@Html.Partial("_TimeframePartial", Model)
</div>
<div>
<div class="form-group">
<div>
<input type="submit" value="Save" class="btn btn-default" />
<input type="button" value="Add Timeframe" class="btn btn-default" id="btnAddTimeframe" />
</div>
</div>
</div>
我的部分观点:
@model Application.Models.NewScheduleRequestModel
@for (var i = 0; i < Model.Request.ResourceRequestScheds.Count; i++)
{
<div class="form-group">
<label class="col-md-2 control-label" for="startDialog">Start</label>
<div class="col-md-4">
@Html.EditorFor(m => m.Request.ResourceRequestScheds[i].StartDateRequested, "dateTextBox")
</div>
<label class="col-md-2 control-label" for="endDialog">End</label>
<div class="col-md-4">
@Html.EditorFor(m => m.Request.ResourceRequestScheds[i].EndDateRequested, "dateTextBox")
</div>
</div>
<div class="form-group row">
<div class="col-md-2"></div>
<div class="col-md-2">
<label class="col-md-12 control-label">Mon</label>
<div class="col-md-12">
@Html.DropDownListFor(m => m.Request.ResourceRequestScheds[i].MonHoursRequested, Model.Hours, new { @class = "form-control" })
</div>
</div>
<div class="col-md-2">
<label class="col-md-12 control-label">Tues</label>
<div class="col-md-12">
@Html.DropDownListFor(m => m.Request.ResourceRequestScheds[i].TuesHoursRequested, Model.Hours, new { @class = "form-control" })
</div>
</div>
<div class="col-md-2">
<label class="col-md-12 control-label">Wed</label>
<div class="col-md-12">
@Html.DropDownListFor(m => m.Request.ResourceRequestScheds[i].WedHoursRequested, Model.Hours, new { @class = "form-control" })
</div>
</div>
<div class="col-md-2">
<label class="col-md-12 control-label">Thurs</label>
<div class="col-md-12">
@Html.DropDownListFor(m => m.Request.ResourceRequestScheds[i].ThursHoursRequested, Model.Hours, new { @class = "form-control" })
</div>
</div>
<div class="col-md-2">
<label class="col-md-12 control-label">Fri</label>
<div class="col-md-12">
@Html.DropDownListFor(m => m.Request.ResourceRequestScheds[i].FriHoursRequested, Model.Hours, new { @class = "form-control" })
</div>
</div>
</div>
<hr />
}
这是我的ajax的开始。这就是我不知道该怎么做,甚至不知道如何真正开始:
$(document).ready(function () {
$("#btnAddTimeframe").click(function () {
$.ajax({
type: 'POST',
});
});
});
我正确地接近这个还是需要重新思考?如果我走在正确的轨道上,那么如果有人可以从这里找出我需要做的事情,那就太棒了。