what is the difference between the using of viewData dictionary and viewData.Model in asp.net mvc -
i need help.
please explain me difference between using of viewdata dictionary , viewdata.model in asp.net mvc.
for example can write
viewdata["list"] = list;
and can write
viewdata.model = list;
so difference between them , when use each one;
explanation example appreciated.
viewdata.model way pass model object controller's action view. can use method ensure view's model strongly-typed, , can reference object using "@model" within view.
@model.someproperty
alternatively (and preferably), pass model object action view using "return view(t);" t = object.
return view(myobject);
lastly, using
viewdata.["somename"] = someobject
would loosely-typed object, , couldn't reference object using @model view. instead, you'd have cast viewdata model-type within view able reference it. example:
// in action: viewdata["obj"] = someobject; // in view: var objdata = viewdata["obj"] myobjectclass // referencing property of object: @objdata.someproperty
so can see there's bit work involved when choose method. personally, don't use viewdata often... viewbag makes more sense me when need send view isn't included part of viewmodel.
Comments
Post a Comment