views:

141

answers:

2

Hi,

I was wondering if it's possible to render an Html Helper in a View inside a codeblock. So instead of:

<% = Html.TextBox("sometextbox", "somethingelse") %>

I want to do:

<% 
switch(SomeParameter) 
{
   case "blah":
       Html.TextBox("sometextbox", "somethingelse")
   break;
}
%>

And have this render. Of course as it is, it wont render, so is there a way to programically decide if a textbox can be added without having to have a million delimiters in the page to accomplish this?

Thanks in advance!

+1  A: 
<% 
    switch(SomeParameter) 
    { 
        case "blah": 
            %><%=Html.TextBox("sometextbox", "somethingelse")%><%
            break; 
    } 
%>

<%= %> is just a shorthand notation for Response.Write() though so the following should work too.

<% 
    switch(SomeParameter) 
    { 
        case "blah": 
            Response.Write(Html.TextBox("sometextbox", "somethingelse"));
            break; 
    } 
%>

All the HtmlHelpers return a string and don't output to the response stream directly by design.

Martijn Laarman
Thank you, that's what I needed, I don't know why I didn't think of that since that's part of the classic model.
sethxian
A: 

Is this what your looking for?

  <% switch (SomeParameter)
       {
           case "blah": %>
    <%= Html.TextBox("sometextbox", "somethingelse") %>
    <% break;
       } %>
mjmcloug
Close, I was trying to avoid having to separate my delimiters for each break. Thank you for your response!
sethxian
Sorry me being simple don't know why I didn't give the solution above :D
mjmcloug
Probably for the same reason I didn't think of it, sometimes we don't process what we know hehe :) Thanks for your time.
sethxian