ビューでオブジェクトのリストを作成しようとしていますが、postメソッドでは、入力されるパラメーターがnullです。作成ページには必要なすべての情報が正しく読み込まれますが、[作成]をクリックすると、タイトルにエラーが表示されます。何が間違っているのでしょうか?
従業員は、毎日彼らにリンクされた可用性を持っています。それが私が達成しようとしていることです
モデル
public class Availability
{
[Required]
public long Id { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "Weekday")]
public string weekday { get; set; }
[DataType(DataType.Time)]
[Display(Name = "Start Time")]
public DateTime StartTime { get; set; }
[DataType(DataType.Time)]
[Display(Name = "End Time")]
public DateTime EndTime { get; set; }
public virtual Employee employee { get; set; }
}
}
カスタムクラス
public class SetAvailability
{
public long EmpID { get; set; }
public String firstName {get; set;}
public String lastName { get; set; }
public Availability availability {get; set;}
}
コントローラメソッド
// GET: /Availability/Create
public ActionResult Create(long id)
{
string[] weekdays ={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
//find the employee
Employee emp = db.Employees.Find(id);
//create a list of setAvailability objects for each day
List<SetAvailability> editor = new List<SetAvailability>();
//instantiate each SetAvailability object and populate accordingly for 7 days
for (int i = 0; i < 7; i++)
{
//create a blank SetAvilability
var _editor = new SetAvailability();
//create a blank availability
_editor.availability = new Availability();
//set the weekday
_editor.availability.weekday = weekdays[i].ToString();
//set the employee id, first name and last name
_editor.EmpID = emp.Id;
_editor.firstName = emp.FirstName;
_editor.lastName = emp.LastName;
//add the _editor to the editorlist
editor.Add(_editor);
}
return View(editor);
}
//
// POST: /Availability/Create
[HttpPost]
public ActionResult Create(List<SetAvailability> dto)
{
//dto object is coming in null! (checked by debugging)
List<SetAvailability> temp = new List<SetAvailability>();
temp = dto;
if (ModelState.IsValid)
{
// set the values for each availability object
// breaks here!
foreach (var item in dto)
{
// get the employee
item.availability.employee = db.Employees.Find(item.EmpID);
// weekday should already be set
// start and end times should come in from the create view
db.Availability.Add(item.availability);
}
db.SaveChanges();
return RedirectToAction("Index","employee");
}
return View(temp);
}
意見
@model List<VolumeV2.Models.DTOs.SetAvailability>
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Availability</legend>
<table>
<tr>
@foreach (var item in Model)
{
@Html.HiddenFor(model => item.EmpID)
@Html.HiddenFor(model => item.firstName)
@Html.HiddenFor(model => item.lastName)
<td>
@Html.LabelFor(model => item.availability.weekday)
</td>
<td>
@Html.LabelFor(model => item.availability.StartTime)
</td>
<td>
@Html.LabelFor(model => item.availability.EndTime)
</td>
break;
}
</tr>
@foreach (var item in Model){
@Html.HiddenFor(model => item.EmpID)
@Html.HiddenFor(model => item.firstName)
@Html.HiddenFor(model => item.lastName)
<tr>
<td>
@Html.EditorFor(modelitem => item.availability.weekday)
@Html.ValidationMessageFor(modelitem => item.availability.weekday)
</td>
<td>
@Html.EditorFor(modelitem => item.availability.StartTime)
@Html.ValidationMessageFor(modelitem => item.availability.StartTime)
</td>
<td>
@Html.EditorFor(modelitem => item.availability.EndTime)
@Html.ValidationMessageFor(modelitem => item.availability.EndTime)
</td>
</tr>
}
</table>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}