简体   繁体   English

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

[英]How to create drop down list in Create.cshtml

How to create drop down list in Create.cshtml, where the list is made of data from DB and if there is no such kind of data you want to choose you can enter new value and it saves in DB. 如何在Create.cshtml中创建下拉列表,该列表由DB中的数据组成,如果没有此类数据,您可以选择输入新值并将其保存在DB中。

I tried to query from DB to a list and used ViewBag.DropDownList (it worked in Index.cshtml, but not in Create.cshtml, because I was getting error: There is no ViewData item of type 'IEnumerable' that has the key "MyDropDownList") 我尝试从数据库查询到一个列表,并使用ViewBag.DropDownList(它在Index.cshtml中有效,但在Create.cshtml中不起作用,因为出现错误:没有“ IEnumerable”类型的ViewData项具有键“ MyDropDownList“)

Create.cshtml: 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") }

and controller CompanyNames.cs: 和控制器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());
    }
}

Can someone help me with this? 有人可以帮我弄这个吗? I just need some kind of direction where to go, to do. 我只需要某种方向去做。 Sorry for the code.... 抱歉,代码...。

model created : 创建的模型:

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

Create a function to bind a list : 创建一个绑定列表的函数:

 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;
        }

On Controller : 在控制器上:

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);
        }

On View: 查看时:

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

I think you need Html.DropDownList instead of ViewBag.DropDownList . 我认为您需要Html.DropDownList而不是ViewBag.DropDownList The former is from the HtmlHelper class and I think it's what you need. 前者来自HtmlHelper类,我认为这是您所需要的。 You can read more about it here . 您可以在此处了解更多信息。

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

相关问题 在index.cshtml页面上是Create.cshtml吗? - Create.cshtml on the index.cshtml page? 如何在 CSHTML 中使用 angular 创建下拉列表? - How do I create a drop down list using angular inside CSHTML? 在 MVC Web 应用程序上调用按钮时如何创建.cshtml? - How to create.cshtml when button is invoked on your MVC web application? MVC NotMapped属性Editor用于在Edit.cshtml中返回空值,但不能在Create.cshtml中返回 - MVC NotMapped attribute EditorFor returning null value in Edit.cshtml but not Create.cshtml 当没有将模型类的实例传递给视图时,如何避免Create.cshtml中的EditorFor(x = x.Prop)生成的null异常? - How to avoid null exception generated by EditorFor(x=x.Prop) in Create.cshtml when no instance of model class is passed to the view? 如何从 register.cshtml 和 register.cshtml.cs 中的 AspNetRoles 表中构建下拉列表? - how to Build drop-down list from AspNetRoles table in register.cshtml and register.cshtml.cs? 如何使用选项下拉列表创建Public String属性? - How to create Public String property with drop-down list of options? 如何在c#中为动态下拉列表创建事件处理程序 - how to create event handler for dynamic drop down list in c# 如何使用窗体形式在C#中创建动态下拉列表 - how to create a dynamic drop down list in C# with window forms 如何在ASP.NET MVC3中创建下拉列表? - How to create a drop down list in ASP.NET MVC3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM