c# - ASP.NET MVC dropdown-list from database -
ok, i'm new whole mvc-world, seems pretty way of getting things done , i'm trying make work here.
the problem is: can't data table in sql-database simple drop-down form on registration page.
i have no idea put stuff, code open table, select ids, put response.write , how send view?
my model this:
public class users { public string name {get; set;} public int user_id {get; set;} } my controller this:
[httpget] public actionresult listuser() { return view(); } and view this:
@model community.models.users i have googled 2 days , watched several videos on youtube of no use, can't find it. please, knowledge here? , please point me tutorials and/or forums can browse more questions might have
still no luck on project..
i'm creating form , within form, want db-loop (ienumerable).. current model not ienumerable. i'm pretty stuck, watched bunch of tutorials , list 1 connection, if want 2 models?
here controller, must pass list view, right?
public actionresult registration() { return view(db.users.tolist()); } how hold of list in view witout ienumerable model?
@neoistheone, example didnt me much, db opens this:
private databasecontext db = new databasecontext(); and don't know how, opens connection. i've tried many hours now, silly, haven't slept soo long!
i'm used programming asp-classic fyi, , first serious try upgrade knowledge programing up-to-date language , oop.
add selectlist model:
public selectlist dropdownlist { get; set; } build class collection:
public class mylisttable { public string key { get; set; } public string display { get; set; } } and in controller, load data mylisttable class database:
var list = new list<mylisttable>(); using (sqlconnection c = new sqlconnection(cstring)) using (sqlcommand cmd = new sqlcommand("select keyfield, displayfield table", c)) { using (sqldatareader rdr = cmd.executereader()) { while (rdr.read()) { list.add(new mylisttable { key = rdr.getstring(0), display = rdr.getstring(1) }); } } } var model = new users(); model.dropdownlist = new selectlist(list, "key", "display"); and finally, need send model view:
return view(model); now in razor can display this:
@html.dropdownlistfor(m => model.dropdownlist); you of course can name these things better names, idea.
Comments
Post a Comment