Html Helper for <ICollection>
本文关键字:ICollection gt Helper lt Html for | 更新日期: 2023-09-27 18:06:01
我有一个视图模型,其中有一个项目是设备的会话,我想创建一个新的会话,但我不确定如何使用HTML helper来实现这一点,这里是视图模型:
public class SessionInsertViewModel
{
public string Title { get; set; }
public string Description { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public int Hour { get; set; }
public int Minute { get; set; }
public int Duration { get; set; }
public string Difficulty { get; set; }
public string Equipment { get; set; }
public virtual ICollection<Product> Products { get; set; }
public int ClassId { get; set; }
}
,这是我看到的Form:
@using (Html.BeginForm(MVC.Session.Insert(), FormMethod.Post, new { @class = "form label-inline", name = "iform", enctype = "multipart/form-data", id = "Insert" }))
{
@Html.HiddenFor(model => model.ClassId)
<div class="formSep">
<label class="req">Session Name</label>
<div style="color:red;display:none" id="reqTitle">This Field is required to create a Session</div>
@Html.EditorFor(model => model.Title, new { @class = "medium", id="Title"})
</div>
<div class="formSep">
<span style="color:red">@Html.ValidationMessageFor(model => model.Description)</span>
<label class="req">Description</label>
<div style="color:red;display:none" id="reqDesc">This Field is required to create a Session</div>
@Html.TextAreaFor(model => model.Description,new{style="width: 420px; height: 6em;"})
</div>
<div class="formSep">
<table>
<tr>
<th style="text-align:left"><span>Date</span></th>
<th style="text-align:left"><div>Time</div></th>
</tr>
<tr>
<th style="padding-right: 20px;"><input id="StartDate" type="text" style="width:120px" /></th>
<th><input id="Hour" value="12:00" type="text" style="width:67px" /></th>
</tr>
</table>
</div>
<div class="formSep">
<label class="req">Duration (In Minutes)</label>
@Html.EditorFor(model => model.Duration, new { @class = "medium", id = "Duration" })
</div>
<div class="formSep">
<label class="req">Difficulty</label>
@Html.DropDownListFor(model => model.Difficulty, new SelectList(new List<Object> { new { value = "Easy", text = "Easy" }, new { value = "Medium", text = "Medium" }, new { value = "Difficult", text = "Difficult" } }, "value", "text", @Model.Difficulty), new { id="Difficulty" })
</div>
</div>
}
所以我需要能够选择设备列表的形式和发送它与ViewModel的控制器,但我不知道如何做到这一点。
将可能的下拉菜单选项保存在模型中,而不是在视图中。
然后你可以通过另一种方式传递它们(即从Controller> Model> View)。
示例:模型:
public class SessionInsertViewModel
{
// existing code
public List<Difficulty> Difficulties { get; set; }
}
视图:
@Html.DropDownListFor( model => model.Difficulty
, new SelectList( Model.Difficulties
, "Text"
, "Value"
)
)