asp.net - Can I pass both the ViewBag AND a Model to a View -
can use hybrid approach passes information in viewbag (c#) / viewdata (vb) includes model
for example, started passing simple title through error controller error view:
function notfound() actionresult response.statuscode = 404 viewdata.add("title", "404 error") return view("error") end function this worked fine , view had access property @viewdata("title")
then wanted include of exception information automatically routed custom errors in handleerrorinfo object this:
function notfound(exception handleerrorinfo) actionresult response.statuscode = 404 viewdata.add("title", "404 error") viewdata.add("message", "sorry, requested page not found.") return view("error", exception) end function then, on view, declared model this:
@modeltype system.web.mvc.handleerrorinfo now can access properties @model.exception.message @viewdata("title") has disappeared.
the webviewpage(of tmodel) still has viewdata property, looks can't pass controller unless it's explicitly in model.
of course, workaround, create base class can type storage each object, seems little heavy handed when want pass in through viewbag title.
is there way use both viewbag , model @ same time?
what happening in case when customerrors tried route notfound action on error controller, supply default constructor. not finding one, bypassed controller altogether , went directly error page had model wanted pass ready.
as small proof of concept, passing both parameters possible evidenced example
testmodel.vb:
public class testmodel public property modelinfo string end class testcontroller.vb:
public class testcontroller : inherits system.web.mvc.controller function index() actionresult dim model new testmodel {.modelinfo = "hello"} viewdata("viewdatainfo") = "world" return view("index", model) end function end class test\index.vbhtml:
@modeltype testmodel @model.modelinfo @viewdata("viewdatainfo")
Comments
Post a Comment