如何在Create.cshtml中创建下拉列表

时间:2015-07-13 14:52:08

标签: c# asp.net asp.net-mvc visual-studio asp.net-mvc-4

如何在Create.cshtml中创建下拉列表,其中列表由来自DB的数据组成,如果没有您想要选择的此类数据,则可以输入新值并将其保存在DB中。

我尝试从DB查询到列表并使用ViewBag.DropDownList(它在Index.cshtml中工作,但在Create.cshtml中没有,因为我收到错误:没有类型'IEnumerable'的ViewData项目关键字“MyDropDownList”)

Create.cshtml:

@using LicenseManager.Models
@model LicenseManager.Models.CompanyNames
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>CompanyNames</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.DropDownList("CompanyNames", (SelectList)ViewBag.DropDownValues)
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval") }

和控制器CompanyNames.cs:

public class CompanyNamesController : Controller
{
    private ApplicationDbContext db = new ApplicationDbContext();

    // GET: CompanyNames
    public ActionResult Index()
    {
        ApplicationDbContext db = new ApplicationDbContext();
        var querys = from c in db.CompanyNames
            select c.Name;

        ViewBag.DropDownValues = new SelectList(querys);
        return View(db.CompanyNames.ToList());
    }
}

有人可以帮我吗?我只是需要某种方向去做。对不起代码......

2 个答案:

答案 0 :(得分:1)

模型创建:

public class CustomerModel
{
  public List<SelectListItem> ListAdCode { get; set; }
}

创建一个绑定列表的函数:

 public static List<SelectListItem> AdCodeList()
        {
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem { Text = "--Select--", Value = null, Selected = true });
            using (CrmKolmEntities entities = new CrmKolmEntities())
            {
                var allActiveCAdCodes = entities.AdCodes.Where(x => x.IsDeleted == false).ToList();
                if (allActiveCAdCodes.Count() > 0)
                {
                    foreach (var adCode in allActiveCAdCodes)
                    {
                        list.Add(new SelectListItem { Text = adCode.AdCodeName, Value = adCode.AdCodeId.ToString() });
                    }
                }
            }
            return list;
        }

在控制器上:

public ActionResult ManipulateCustomer(int id, int? idOrder)
        {
            CustomerModel model = new CustomerModel();

            model.ListAdCode = AdCodeList();

            if (model.ListPriceList == null)
            {
                model.ListPriceList = CommonFunctions.PriceListActive(null);
            }
            return View(model);
        }

观看视频:

 @Html.DropDownListFor(r => r.AdCodeId, new SelectList(Model.ListAdCode, "value", "text", "selectedValue"), new { @class = "input", @style = "width:450px" })

答案 1 :(得分:0)

我认为您需要Html.DropDownList而不是ViewBag.DropDownList。前者来自HtmlHelper类,我认为它是你需要的。您可以阅读更多相关信息here