asp.net - MVC C# Session Project -
i posted question no 1 answered comments/errors had. how text entered in @html.textbox("searchstring") in index viw welcome view using session variables?
homecontroller.cs
using system; using system.collections.generic; using system.linq; using system.web; using system.web.mvc; using system.data.entity; using newproject.models; namespace newproject.controllers { public class homecontroller : controller { public actionresult index() { return view(); } [httppost] public actionresult gender(welcomevm model) { model.genders = new list<selectlistitem> { new selectlistitem { value="male", text="male"}, new selectlistitem { value="female", text="female"} }; return view(model); } [httppost] public actionresult welcome(welcomevm model) { return view(model); } public object final { get; set; } } }
index.cshtml
@{ viewbag.title = "welcome"; } <br /> @html.label("name") <br /> @using (html.beginform("gender", "home", formmethod.post)) { @html.textbox("name"); <input id="btnsubmit" name="btnsubmit" placeholder="test" type="submit" value="submit" /> }
gender.cs
using system.collections.generic; using system.web.mvc; public class welcomevm { public string name { set; get; } public string gender { set; get; } public ienumerable<selectlistitem> genders { set; get; } }
gender.cshtml
@model welcomevm @using (html.beginform("welcome", "home", formmethod.post)) { <p>hello @model.name gender</p> @html.dropdownlistfor(m => m.gender, model.genders) @html.hiddenfor(s => s.name) <input type="submit" /> }
welcome.cshtml
@{ viewbag.title = "welcome"; } @model welcomevm <p>hello @model.name @model.gender</p>
you not need session or viewbag pass data around. can use view model have.
so in index view,
@html.label("name") @using (html.beginform("gender", "home")) { @html.textbox("name"); <input id="btnsubmit" name="btnsubmit" type="submit" value="submit" /> }
and in gender
action method use same view model parameter type. when form posted, default model binder map posted form values ot object of welcomevm
class.
[httppost] public actionresult gender(welcomevm model) { model.genders = new list<selectlistitem> { new selectlistitem { value="male", text="male"}, new selectlistitem { value="female", text="female"} }; return view(model); }
and in gender.cshtml
view again typed same view model.
@model welcomevm @using (html.beginform("welcome", "home", formmethod.post)) { <p>hello @model.name gender</p> @html.dropdownlistfor(m => m.gender, model.genders) @html.hiddenfor(s=>s.name) <input type="submit" /> }
and in welcome action
[httppost] public actionresult welcome(welcomevm model) { return view(model); }
and in welcome.cshtml view,which again typed view model,
@model welcomevm <p>hello @searchstring @model.gender</p>
Comments
Post a Comment