Vì vậy, vấn đề của tôi ngay bây giờ là tôi không thể có được mô hình của tôi vào bộ điều khiển của tôi khi tôi gửi mẫu sau đây. Tôi đang cố gắng để có các mục từ các BillingCodes (đó là một danh sách các BillingCodeObjects) vòng lặp thông qua và hiển thị. Tôi đã xóa một số mã khỏi những điều này không thực sự liên quan đến tình huống để làm cho nó ngắn hơn và dễ đọc hơn.Gửi mẫu trong mô hình MVC 4 là null trong phương pháp điều khiển bài
Đây là mã cho quan điểm của tôi ...
@using (Html.BeginForm("SubmitTimesheet", "Timesheet", FormMethod.Post))
{
foreach (var item in Model.BillingCodes)
{
<div class="button-padding">
<a class="btn btn-large btn-danger btn-block billCodeBtn">
<div class="btnText">@item.Name</div>
<div class="btnTime">@item.TotalHours</div>
<i class="icon-chevron-down billCodeIconUp billCodeIcon"></i>
<i class="hidden icon-chevron-up billCodeIconDown billCodeIcon"></i>
</a>
<div class="content" >
<div class="row timeEntry">
<p></p>
<div class="col-12 col-lg-2">
Enter Time:
<div class="row">
<div class="col-12">
@Html.DropDownListFor(model => item.EnterTimeHours, new SelectList(new[] {
new { Value = "0", Text = "0" },
new { Value = "1", Text = "1" },
new { Value = "2", Text = "2" },
new { Value = "3", Text = "3" },
new { Value = "4", Text = "4" },
new { Value = "5", Text = "5" },
new { Value = "6", Text = "6" },
new { Value = "7", Text = "7" },
new { Value = "8", Text = "8" },
new { Value = "9", Text = "9" },
new { Value = "10", Text = "10" },
new { Value = "11", Text = "11" },
new { Value = "12", Text = "12" },
}, "Value", "Text")) <b>:</b>
@Html.DropDownListFor(model => item.EnterTimeMinutes, new SelectList(new[] {
new { Value = "0", Text = "00" },
new { Value = "15", Text = "15" },
new { Value = "30", Text = "30" },
new { Value = "45", Text = "45" },
}, "Value", "Text"))
</div>
</div>
</div>
<div class="col-lg-2"></div>
<div class="col-lg-1"></div>
<div class="control-group col-12 col-lg-2">
<label class="checkbox">
Billable @Html.CheckBoxFor(model => item.Billable)
</label>
</div>
<div class="col-12 col-lg-2">
Enter Memo:
<div class="row">
<div class="col-12">
@Html.TextAreaFor(model => item.Comment)
</div>
</div>
Và đây là một số mã cho bộ điều khiển của tôi:
public class TimesheetController : Controller
{
//
// GET: /Timesheet/
public ActionResult Index()
{
string getBillingCodeUrl ="";
//SOME CODE REMOVED FOR LENGTH/READABILITY
foreach (var entryItem in timePeriod.TimeEntries[0].EntryCollection)
{
foreach (var billingItem in billingCodeList.BillingCodes)
{
if (entryItem.BillingCode == billingItem.Name)
{
//update record in billingItem with data from entryItem
billingItem.Comment = entryItem.Comment;
billingItem.Billable = entryItem.Billable;
billingItem.TotalHours = entryItem.Hours;
}
}
}
return View(billingCodeList);
}
[HttpPost]
public void SubmitTimesheet(BillingCodeList model)
{
string uri = "";
foreach (var billingCode in model.BillingCodes)
{
//do stuff with each of these
}
}
}
}
và cuối cùng, đây là những thông tin đó là trong mô hình:
public class BillingCodeList
{
public List<BillingCodeObj> BillingCodes;
}
public class BillingCodeObj
{
public string Name { get; set; }
public decimal TotalHours { get; set; }
public decimal EnterTimeHours { get; set; }
public decimal EnterTimeMinutes { get; set; }
public bool Billable { get; set; }
public string Comment { get; set; }
public BillingCodeObj(string name, decimal hours)
{
this.Name = name;
this.TotalHours = hours;
}
public BillingCodeObj()
{
}
}
đây là hình ảnh của người gỡ lỗi khi biểu mẫu được trả lại ..
Ok, cảm ơn.Điều đầu tiên không hoạt động, đối tượng billingCodes vẫn là null. Vì vậy, tôi sẽ mất một thời gian và kiểm tra hình thức nó trả về. –
nếu bạn có thể đăng các đầu vào được tạo ra trên html, tôi sẽ vui vẻ giúp bạn. –
okay Tôi đã chỉnh sửa bài đăng gốc và bao gồm ảnh của người dân địa phương cũng ở đây: [link] (http://i.imgur.com/0KXBDXr.png) –