简体   繁体   English

如何在 asp.net mvc 中创建 DropdownlistFor?

[英]How to create a DropdownlistFor in asp.net mvc?

I am creating a dropdownlistfor, but somehow its throwing no Viewdata exception on my View and need some help to resolve it.我正在创建一个下拉列表,但不知何故它在我的视图上没有抛出任何 Viewdata 异常,需要一些帮助来解决它。 Below is my logic to try and achieve it and somehow i am unfortunate to do it.以下是我尝试实现它的逻辑,但不知何故我不幸做到了。

// Model
// This is for course-list.
public class eNtsaDashboardViewModel
{
   public string CourseName { get; set; }
   public string CourseLicence { get; set; }
   public string Course_LicenceName {get;set;}
}

// Controller
public static IEnumerable<eNtsaDashboardViewModel> eNtsaDashboardViews = new List<eNtsaDashboardViewModel>
{
    new eNtsaDashboardViewModel
    {
        CourseLicence = "Private(Copyright)",
        Course_LicenceName = "Public Domain, " +
        "Creative Commons Licencese," +
        "CC Attribution, " +
        "CC Attribution Share Alike, " +
        "CC Attribution Non Commercial, " +
        "CC Attribution Non Commercial Alike, " +
        "CC Attribution No Directives, " +
        "CC Attribution Non-Commercial No Directive"
    }
};

//View
<div class="form-group">
    <label for="CourseName" class="col-sm-3 col-form-label">CourseName</label>
    <div class="col-sm-5">
        @Html.EditorFor(model => model.CourseName, new { htmlAttributes = new { @class = "form-control", autofocus = "autofocus", placeholder = "CourseName" } })
        <label for="Content Licence" class="col-sm-3 col-form-label">Content Licence</label>
        @Html.DropDownListFor(model=>model.CourseLicence, ViewBag.Course_LicenceName as SelectList) // Throws exception here for noViewData.
    </div>
</div>

just play around with the code below:只需使用以下代码即可:

@Html.DropDownListFor(model => model.CourseLicence,
        new SelectList(ViewBag.Course_LicenceName, "CourseLicence", "Course_LicenceName", "--"), null,
        new { @class = "form-control" })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM