0

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!

2
  • Should you be putting logic like that in your view? Commented Jan 20, 2010 at 23:19
  • I'm still toying with the design. At some point it has to end up int he view, the Html Elements int his case are driven from a database table, so I'm not really sure where it could go and still end up int he view. I may have 1 to 10 parameters programically added depending on what's in the table. Commented Jan 20, 2010 at 23:51

2 Answers 2

1
<% 
    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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
0

Is this what your looking for?

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

3 Comments

Close, I was trying to avoid having to separate my delimiters for each break. Thank you for your response!
Sorry me being simple don't know why I didn't give the solution above :D
Probably for the same reason I didn't think of it, sometimes we don't process what we know hehe :) Thanks for your time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.