在视图中我有下一个结构(每个 *组的主题* s的控制权):
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@for (int i = 0; i < ViewBag.AllGroups.Count; i++)
{
<h4>@ViewBag.AllGroups[i].Code</h4>
<select id="e-@i" multiple="multiple">
@foreach (Subject subject in ViewBag.AllSubjects)
{
<option value="@subject.Name">@subject.Name</option>
}
</select>
}
<input type="submit" value="Generate" class="btn btn-default" />
</div>
}
问题是我如何才能检索这些数据(我希望收到(1)群组列表,并且我想获得所有选定主题的(2)列表我的名单中的每个小组(1))在我的控制器中?
提前谢谢。
答案 0 :(得分:4)
推荐的方法是使用强类型的View Model Object
public class GroupViewModel
{
public string Code { get;set; }
public List<Subject> AllSubjects { get; set; }
}
将List作为模型传递给控制器中的Razor视图。
return new View(new List<GroupViewModel>()); // populated one.
在视图中使用此列表。
@model IList<GroupViewModel>
@for (int i = 0; i < Model.Count; i++)
{
<h4>Model[i].Code</h4>
<select id="e-@i" multiple="multiple">
@foreach (Subject subject in Model[i].AllSubjects)
{
<option value="@subject.Name">@subject.Name</option>
}
</select>
}
答案 1 :(得分:0)
除了您错过了选择元素的标记名称之外,没有什么特别可以处理这种情况。
确切地说,您在此处使用的所有html元素(例如 select )应该具有不是id (id =“e- @ i”)的名称,并且所有元素根据其名称进行序列化并发送到服务器。另一方面,在服务器端,您应该获取csv格式的已发布值(由于添加了多个功能,您已添加到select元素)
答案 2 :(得分:0)
通过简化任务解决了我的问题。我必须要做的:我为这件事创造了新的ViewModel。我将标签<select></select>
替换为@Html.ListBoxFor(m => m.Subjects, Model.SubjectItems)
。我必须在我的ViewModel中创建一个SubjectItems
列表。
这是代码(很抱歉代码很多:我只是希望一切都清楚):
我的观点:
@using System
@using System.Linq
@using TimeTable.GenericRepository
@model TimeTable.Models.GroupViewModel
@{
//it's better to move the next line to a Controller later
ViewBag.GroupId = new SelectList(new GroupRepository().Get().ToList(), "Id", "Code", Model.GroupId);
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_LayoutBootstrap.cshtml";
}
<h2>Index</h2>
<hr />
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.GroupId, "Group is: ", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("GroupId", String.Empty)
@Html.ValidationMessageFor(model => model.GroupId)
</div>
</div>
@Html.ListBoxFor(m => m.Subjects, Model.SubjectItems, new { @id = "e", @style = "width:80%; " })
<br /><br />
<input type="submit" value="Generate" class="btn btn-default" />
</div>
}
@* ReSharper disable once Razor.SectionNotResolved *@
@section Scripts {
@Styles.Render("~/Content/select2")
@Scripts.Render("~/bundles/select2")
<script type="text/javascript">
$(function () { $("#e").select2(); });
</script>
}
我的控制器:
public ActionResult Generate()
{
return View(new GroupViewModel());
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Generate(GroupViewModel gvm)
{
var subjects= gvm.Subjects; // <== selected subjects are here
return View();
}
我的模特:
public class GroupViewModel
{
public int GroupId{ get; set; }
public Group Group {
get { return new GroupRepository().GetById(GroupId); }
}
public object Subjects { get; set; }
public IEnumerable<SelectListItem> SubjectItems
{
get
{
var items = new SelectList(new SubjectRepository().Get().ToList(), "Id", "Name");
return items;
}
}
}
P.S。 Select2是selectBoxes的自定义替代品,没有必要,但我喜欢它:http://ivaynberg.github.io/select2/