c# - How to create drop down list in Create.cshtml -


how create drop down list in create.cshtml, list made of data db , if there no such kind of data want choose can enter new value , saves in db.

i tried query db list , used viewbag.dropdownlist (it worked in index.cshtml, not in create.cshtml, because getting error: there no viewdata item of type 'ienumerable' has 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 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 = c in db.companynames             select c.name;          viewbag.dropdownvalues = new selectlist(querys);         return view(db.companynames.tolist());     } } 

can me this? need kind of direction go, do. sorry code....

model created :

public class customermodel {   public list<selectlistitem> listadcode { get; set; } } 

create function bind 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" }) 

Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -