简体   繁体   中英

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.

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")

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:

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 . The former is from the HtmlHelper class and I think it's what you need. You can read more about it here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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