首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 ><ICollection>的Helper

<ICollection>的Helper
EN

Stack Overflow用户
提问于 2014-05-30 14:58:25
回答 1查看 183关注 0票数 1

我有一个视图模型,其中有一些项是会话的设备,我想创建一个新的会话,但是我不知道如何使用HTML助手来实现这一点,下面是视图模型:

代码语言:javascript
复制
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; }

}

以下是我的观点:

代码语言:javascript
复制
 @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一起发送到控制器,但我不知道如何做到这一点。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-30 15:06:00

在模型中保存可能的下拉选项,而不是在视图中。

然后,您可以以另一种方式传递它们(从Controller > Model > View)。

示例:

型号:

代码语言:javascript
复制
public class SessionInsertViewModel
{
    // existing code

    public List<Difficulty> Difficulties { get; set; }
}

查看:

代码语言:javascript
复制
@Html.DropDownListFor( model => model.Difficulty
                     , new SelectList( Model.Difficulties
                                     , "Text"
                                     , "Value"
                                     )
                     )
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23957588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档