Dumb question, but I can't find the namespace where it's stored - in the view code I can execute Html.TextBox(<name>, <contents>, <html attribs>) and Html.DropDownList(<name>, <select list contents>, <html attribs>) but how do I execute these methods in
controller code?
In case you are wondering why on earth I want to do this, rather than using the TextBox object, I execute Html.TextBox(....).ToString() and create html code from these objects.
Cheers
MH
Please remember to mark replies as answers if you find them useful =8)
It's not very straightforward. This willk work in MVC 2:
TextWriter tw = TextWriter.Null;
var helper = new HtmlHelper(new ViewContext(this.ControllerContext, new WebFormView("asd"), new ViewDataDictionary(), new TempDataDictionary(), tw), new ViewPage());
ViewData["Message"] = helper.TextBox("name");
You may ned to change the ViewContext constructor params for mvc 1. Since it's such a pain to do all that, I'd just type in <input type='text'... />
Hope that helps.
"If I can see further than anyone else, it is only because I am standing on the shoulders of giants."blog: www.heartysoft.com twitter: @ashic
Marked as answer by mad-halfling on Feb 18, 2010 08:47 AM
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Thanks, that's what I needed. Actually, that's a lot more simple than typing the html code as I am creating DropDownLists and Checkbox objects with this too (the controller method is an AJAX called one that is returning html), and I needed to keep the returned
objects consistent with any generated on the view (esp WRT the way .net creates checkbox elements).
Please remember to mark replies as answers if you find them useful =8)
This is not a good idea, because eventually something that uses this will throw. Instead, you should use "Response.Output" as the text writer to pass to the ViewContext constructor.
Actually, I'd avoid the whole construct. The point is to get the input tag string from the helper. If we were to write to Response.Output, it'll output directly to the response. What if the request is an Ajax (or more correctly, an AJAH) request? Wouldn't
that break the response format entirely? As such, it'd be better to throw than to write directly to the output from the controller.
"If I can see further than anyone else, it is only because I am standing on the shoulders of giants."blog: www.heartysoft.com twitter: @ashic
Revisiting the topic Brad, TextWriter.Null does not pass in a null, it's a dummy textwriter that can be written to but not read from. As such, any helper writing to the textwriter will be able to write perfectly. Does any Html helper actually need to read
form the textwriter?
"If I can see further than anyone else, it is only because I am standing on the shoulders of giants."blog: www.heartysoft.com twitter: @ashic
This is not a good idea, because eventually something that uses this will throw. Instead, you should use "Response.Output" as the text writer to pass to the ViewContext constructor.
What I ended up using (I eventually found something similar elsewhere) was:-
HtmlHelper htmlHelper = new HtmlHelper(new ViewContext(ControllerContext,
new WebFormView("tempView"),
new ViewDataDictionary(),
new TempDataDictionary()), new ViewPage());
- this seems to work OK, will it cause any problems?
Cheers
MH
Please remember to mark replies as answers if you find them useful =8)
Marked as answer by mad-halfling on Apr 14, 2010 11:17 AM
Mad-Halfling
Participant
1438 Points
729 Posts
Executing Html.TextBox in controller code
Feb 17, 2010 05:57 PM|LINK
Dumb question, but I can't find the namespace where it's stored - in the view code I can execute Html.TextBox(<name>, <contents>, <html attribs>) and Html.DropDownList(<name>, <select list contents>, <html attribs>) but how do I execute these methods in controller code?
In case you are wondering why on earth I want to do this, rather than using the TextBox object, I execute Html.TextBox(....).ToString() and create html code from these objects.
Cheers
MH
HeartattacK
All-Star
55262 Points
5917 Posts
Moderator
MVP
Re: Executing Html.TextBox in controller code
Feb 18, 2010 04:41 AM|LINK
It's not very straightforward. This willk work in MVC 2:
TextWriter tw = TextWriter.Null; var helper = new HtmlHelper(new ViewContext(this.ControllerContext, new WebFormView("asd"), new ViewDataDictionary(), new TempDataDictionary(), tw), new ViewPage()); ViewData["Message"] = helper.TextBox("name");You may ned to change the ViewContext constructor params for mvc 1. Since it's such a pain to do all that, I'd just type in <input type='text'... />
Hope that helps.
blog: www.heartysoft.com
twitter: @ashic
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Executing Html.TextBox in controller code
Feb 18, 2010 08:45 AM|LINK
http://forums.asp.net/t/1355902.aspx
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Mad-Halfling
Participant
1438 Points
729 Posts
Re: Executing Html.TextBox in controller code
Feb 18, 2010 08:46 AM|LINK
Thanks, that's what I needed. Actually, that's a lot more simple than typing the html code as I am creating DropDownLists and Checkbox objects with this too (the controller method is an AJAX called one that is returning html), and I needed to keep the returned objects consistent with any generated on the view (esp WRT the way .net creates checkbox elements).
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: Executing Html.TextBox in controller code
Feb 18, 2010 04:19 PM|LINK
This is not a good idea, because eventually something that uses this will throw. Instead, you should use "Response.Output" as the text writer to pass to the ViewContext constructor.
HeartattacK
All-Star
55262 Points
5917 Posts
Moderator
MVP
Re: Executing Html.TextBox in controller code
Feb 18, 2010 05:03 PM|LINK
Actually, I'd avoid the whole construct. The point is to get the input tag string from the helper. If we were to write to Response.Output, it'll output directly to the response. What if the request is an Ajax (or more correctly, an AJAH) request? Wouldn't that break the response format entirely? As such, it'd be better to throw than to write directly to the output from the controller.
blog: www.heartysoft.com
twitter: @ashic
HeartattacK
All-Star
55262 Points
5917 Posts
Moderator
MVP
Re: Executing Html.TextBox in controller code
Feb 19, 2010 04:51 AM|LINK
Revisiting the topic Brad, TextWriter.Null does not pass in a null, it's a dummy textwriter that can be written to but not read from. As such, any helper writing to the textwriter will be able to write perfectly. Does any Html helper actually need to read form the textwriter?
blog: www.heartysoft.com
twitter: @ashic
Mad-Halfling
Participant
1438 Points
729 Posts
Re: Executing Html.TextBox in controller code
Feb 19, 2010 08:11 AM|LINK
What I ended up using (I eventually found something similar elsewhere) was:-
HtmlHelper htmlHelper = new HtmlHelper(new ViewContext(ControllerContext, new WebFormView("tempView"), new ViewDataDictionary(), new TempDataDictionary()), new ViewPage());- this seems to work OK, will it cause any problems?
Cheers
MH
HeartattacK
All-Star
55262 Points
5917 Posts
Moderator
MVP
Re: Executing Html.TextBox in controller code
Feb 19, 2010 10:00 AM|LINK
It won't work in MVC 2 as that ctor of ViewContext doesn't exist in MVC 2.
blog: www.heartysoft.com
twitter: @ashic
Mad-Halfling
Participant
1438 Points
729 Posts
Re: Executing Html.TextBox in controller code
Feb 19, 2010 01:43 PM|LINK
Is there a bit of code that works in both versions 1 and 2?