我有一个视图模型,其中有一个项目是会话的设备,我想创建一个新的会话,但我不知道如何使用HTML帮助程序来实现这一点,这里是视图模型:
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; }
}
这是我视图中的表单:
@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一起发送到控制器,但我不知道该怎么做。
答案 0 :(得分:0)
保存模型中下拉列表的可能选项,而不是在视图中。
然后你可以用另一种方式传递它们(所以从Controller&gt; Model&gt; View)。
样品:
型号:
public class SessionInsertViewModel
{
// existing code
public List<Difficulty> Difficulties { get; set; }
}
查看:
@Html.DropDownListFor( model => model.Difficulty
, new SelectList( Model.Difficulties
, "Text"
, "Value"
)
)