我正在使用一个对象但在对象内部我有一个属性是对象列表。我想通过该对象与其中的列表对象,但列表是空的,当我提交我的表单。 我不知道如何处理这个。使用MVC Razor传递列表对象
类
public class VSTAttendance
{
public int MemberID { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public bool Attendant { get; set; }
}
public class ServiceAttendance
{
public int AttendanceID { get; set; }
public int AttendanceTypeID { get; set; }
public string AttendanceType { get; set; }
public DateTime Service { get; set; }
public int ServiceID { get; set; }
public string Speaker { get; set; }
public string Location { get; set; }
public int HeadCount { get; set; }
public int CategoryID { get; set; }
public string Description { get; set; }
public string ChurchNotes { get; set; }
public string ServingNotes { get; set; }
public DateTime DateCreated { get; set; }
public DateTime CreatedBy { get; set; }
public DateTime DateUpdate { get; set; }
public DateTime UpdateBy { get; set; }
public List<VSTAttendance> MemberAttendance { get; set; }
}
HTML
@using (Html.BeginForm("Attendance", "Home", FormMethod.Post, new { ReturnUrl = ViewBag.ReturnUrl, @class = "form-horizontal", role = "form" }))
{
<div class="form-group">
<label for="inputSpeaker" class="col-sm-2 control-label">
Speaker</label>
<div class="col-sm-6">
@Html.TextBoxFor(m => m.Speaker, new { @class = "form-control", placeholder = "Speaker", type = "text", required = "required " })
</div>
</div>
<table class="table table-bordered table-condensed table-hover">
<tr class="warning">
<td></td>
<td>Name</td>
<td>Surname</td>
</tr>
@foreach (var item in Model.MemberAttendance)
{
<tr>
<td>@Html.CheckBox("Attendant")</td>
<td>@item.Name</td>
<td>@item.Surname</td>
</tr>
}
</table>
<div class="form-group">
<div class="col-sm-10">
<button type="submit" class="btn btn-info">
<span class="glyphicon glyphicon-ok"></span>Submit</button>
</div>
</div>
}
为什么我需要使用HiddenFor?它的目的是什么? – Gericke
考虑在你的循环中使用局部视图,会让你的代码更清洁。 –
这帮了我。此外,我的对象是一个ICollection,我必须将其更改为List,以便模型正确传回控制器。 –