asp.net mvc - MVC4 Passing model from view to controller -


i have view model populated data relating booking taxi.

in model list of quotations time, price, vehicle type in display list of using foreach. each time foreah loops builds form , submit button take me "bookingstage1" action in controller. have added hidden field populated bookingrefernce particular quotation.

so, hoping when hit action result in controller model returned populated view. it's null, no data in whatsoever.

i hoping pass populated model between number of controllers user progresses through various search, results , booking screens...

is possible pass populated model view next controller?

thanks

in search results page have following form:

using (html.beginform("bookingpage1", "searchresults", formmethod.post)) 

i have hidden field in form below:

<input type="hidden" id="bookingid" name="chosenbookingid" value='@item.quotationid' /> 

which posts controller looks this:

[httppost]     public actionresult bookingpage1(string chosenbookingid, route theroute)     {         //this noting yet.         return view();     } 

but theroute empty :(

i hope complete example you.

this taxiinfo class holds information taxi ride:

namespace taxi.models {     public class taxiinfo     {         public string driver { get; set; }         public double fare { get; set; }         public double distance { get; set; }         public string startlocation { get; set; }         public string endlocation { get; set; }     } } 

we have convenience model holds list of taxiinfo(s):

namespace taxi.models {     public class taxiinfoset     {         public list<taxiinfo> taxiinfolist { get; set; }          public taxiinfoset(params taxiinfo[] taxiinfos)         {             taxiinfolist = new list<taxiinfo>();              foreach(var taxiinfo in taxiinfos)             {                 taxiinfolist.add(taxiinfo);             }         }     } } 

now in home controller have default index action example makes 2 taxi drivers , adds them list contained in taxiinfo:

public actionresult index() {     var taxi1 = new taxiinfo() { fare = 20.2, distance = 15, driver = "billy", startlocation = "perth", endlocation = "brisbane" };     var taxi2 = new taxiinfo() { fare = 2339.2, distance = 1500, driver = "smith", startlocation = "perth", endlocation = "america" };      return view(new taxiinfoset(taxi1,taxi2)); } 

the code view follows:

@model taxi.models.taxiinfoset @{     viewbag.title = "index"; }  <h2>index</h2>  @foreach(var taxiinfo in model.taxiinfolist){     <form>         <h1>cost: $@taxiinfo.fare</h1>         <h2>distance: @(taxiinfo.distance) km</h2>         <p>             our diver, @taxiinfo.driver take @taxiinfo.startlocation @taxiinfo.endlocation         </p>         @html.actionlink("home","booking",taxiinfo)     </form> } 

the actionlink responsible re-directing booking action of home controller (and passing in appropriate taxiinfo object) defiend follows:

    public actionresult booking(taxiinfo taxi)     {         return view(taxi);     } 

this returns following view:

@model taxi.models.taxiinfo  @{     viewbag.title = "booking"; }  <h2>booking for</h2> <h1>@model.driver, going @model.startlocation @model.endlocation (a total of @model.distance km) $@model.fare</h1> 

a visual tour:

the index view

the booking view


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -