我有一个视图模型,其中有一些项是会话的设备,我想创建一个新的会话,但是我不知道如何使用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一起发送到控制器,但我不知道如何做到这一点。
发布于 2014-05-30 15:06:00
在模型中保存可能的下拉选项,而不是在视图中。
然后,您可以以另一种方式传递它们(从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"
)
)https://stackoverflow.com/questions/23957588
复制相似问题