i new in mvc. got code , populate viewdata controller , pass view. have @ code.
controller
public actionresult edit(int id) { viewdata["name"] = "my name xyz"; return view(); }
view page
@html.textbox("name")
my question how textbox understand extract name , display textbox value. name stored in viewdata. first need extarct data viewdata , can assign data textbox value. how works ?.
some 1 explain things not clear me
mvc designed default protocol keywords , try make associations keywords. you've chosen use 'name' temporary data object. mvc make effort elements have same name. believes intended assign element same name viewdata object because want associated.
the same goes viewbag. can give viewbag name (viewbag.name) , time use 'name' element, mvc automatically assume want element associated viewbag data. works lists too!
viewbag.mylist = db.products.tolist(); @html.dropdownlist("mylist");
my understand mvc flow not clear.i new in mvc. reading mvc tutorial. not convienced after reading above answer. think asp.net web form. in asp.net web form
if store viewstate or session way viewstate["name"]="mou" or session["name"]="mou"
need render value @ client side way <asp:textbox value=<% name %>/>
error if write way <asp:textbox value=<% viewstate["name"] string %>/>
work. how mvc can understand if specify @html.textbox("name")
suppose name can stored in session, viewdata, viewbag, tempdata etc how mvc engine can resolve ? looking explanation more example. thanks
my question how textbox understand extract name , display textbox value.
it's magic strings. have placed name
key viewdata
:
viewdata["name"] = "my name xyz";
so when use @html.textbox("name")
helper search name key in viewdata , automatically bind data.
it's normal seems confusing you. don't use viewbag/viewdata. use view models instead. way no longer relying on magic strings. use typed helpers. example let's suppose wanted bind simple textbox. go ahead , write view model it:
public class myviewmodel { public string name { get; set; } }
then have controller action pass model view:
public actionresult edit(int id) { var model = new myviewmodel(); model.name = "my name xyz"; return view(model); }
and make view typed model , use typed helpers:
@model myviewmodel ... @html.textboxfor(x => x.name)
you see, mvc means model/view/controller. doesn't mean viewdata/view/controller or viewbag/view/controller. many people confusing these abbreviation , defeating purpose of pattern. feeding data view different sources, not view model information should fed. don't people.
and since talked lists, once again writing following wrong:
@html.dropdownlist("mylist")
the reason why wrong because in order have dropdown need 2 properties on model: simple scalar property bind selected value , collection property bind available values in drop down.
just that:
public class myviewmodel { public string selectedvalue { get; set; } public ienumerable<selectlistitem> values { get; set; } }
which usual populated in controller:
public actionresult edit(int id) { var model = new myviewmodel(); // comes database model.values = new[] { new selectlistitem { value = "1", text = "item 1" }, new selectlistitem { value = "2", text = "item 2" }, new selectlistitem { value = "3", text = "item 3" }, }; // allows preselect second value in dropdown model.selectedvalue = "2"; return view(model); }
and in view:
@model myviewmodel ... @html.dropdownlistfor(x => x.selectedvalue, model.values)
so advice sooner forget viewdata/viewbag better.
Comments
Post a Comment