asp.net mvc - How to use ViewBag to pass list of data from View to controller -
how can sen list of items view controller save it. believe can use viewbag dont realy no how use ite pass data view controller.
this have tried view
@using (html.beginform()) { @html.validationsummary(true) <fieldset> <legend>productionorderitem</legend> <div class="editor-label"> @html.label("producrionorderno"); </div> <div class="editor-field"> @html.textbox("productionorderno", viewbag.productionorder int) </div> <div class="editor-label"> @html.label("ordername") </div> <div class="editor-field"> @html.textbox("ordername", viewbag.productionorder string) </div> <div class="editor-label"> @html.label("orderdate") </div> <div class="editor-field"> @html.textbox("orderdate", viewbag.productionorder datetime) </div> <p> <input type="submit" value="create" /> </p> </fieldset> }
and controller
[httppost] public actionresult create(formcollection collection) { productionregistration pr = new productionregistration(); productionitem poi = new productionitem(); poi = viewbag.productionorder; pr.saveorder(conn, poi); return redirecttoaction("index"); }
you can't pass data viewbag/viewdata to controller. it's one-way (controller view). way data controller post (post-body) or send along in querystring.
in fact, should avoid viewbag as possible. added convenience , convenience methods, it's abused more not. use view model both pass data view , accept data post.
you strongly-type view with:
@model namespace.for.my.orderviewmodel
then, can use [foo]for
methods of razor build fields in strongly-typed way:
<div class="editor-label"> @html.labelfor(m => m.productionorderno); </div> <div class="editor-field"> @html.textboxfor(m => m.productionorderno) </div>
and in post action, accept view model param:
[httppost] public actionresult create(orderviewmodel model) { ... }
and let mvc's modelbinder wire posted data you.
no more dynamics. strongly-typed end-to-end, if goes wrong, you'll know @ compile-time, not run-time.
Comments
Post a Comment