gpt4 book ai didi

c# - 使用 MVC Razor 传递列表对象

转载 作者:太空狗 更新时间:2023-10-29 20:52:52 25 4
gpt4 key购买 nike

我正在使用一个对象,但在对象内部我有一个属性,它是一个对象列表。我想传递其中包含列表对象的对象,但是当我提交表单时列表是空的。我不确定如何处理。

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>

}

最佳答案

您必须使用 for 而不是 foreach 并使用 Hidden for 。我已经测试了以下并且工作完美

 @model ServiceAttendance

@{
ViewBag.Title = "Attendance";
}

<h2>Attendance</h2>

@using (Html.BeginForm())
{
<div class="form-group">
<label for="inputSpeaker" class="col-sm-2 control-label">
Speaker
</label>
<div class="col-sm-6">
@Html.TextBoxFor(modelItem => modelItem.Speaker)
</div>
</div>

<table class="table table-bordered table-condensed table-hover">
<tr class="warning">
<td></td>
<td>Name</td>
<td>Surname</td>
</tr>
@for (int i=0;i<Model.MemberAttendance.Count ;i++)
{
<tr>
<td>@Html.CheckBoxFor(it=>Model.MemberAttendance[i].Attendant)
@Html.HiddenFor(it => Model.MemberAttendance[i].Attendant)
</td>
<td>@Model.MemberAttendance[i].Name
@Html.HiddenFor(it => Model.MemberAttendance[i].Name)</td>
<td>@Model.MemberAttendance[i].Surname
@Html.HiddenFor(it=>Model.MemberAttendance[i].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>

}

所以你要做的就是将 foreach 替换为以下内容

 @for (int i=0;i<Model.MemberAttendance.Count ;i++)
{
<tr>
<td>@Html.CheckBoxFor(it=>Model.MemberAttendance[i].Attendant)
@Html.HiddenFor(it => Model.MemberAttendance[i].Attendant)
</td>
<td>@Model.MemberAttendance[i].Name
@Html.HiddenFor(it => Model.MemberAttendance[i].Name)</td>
<td>@Model.MemberAttendance[i].Surname
@Html.HiddenFor(it=>Model.MemberAttendance[i].Surname)</td>
</tr>
}

关于c# - 使用 MVC Razor 传递列表对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23552931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com