sethxian
sethxian

Reputation: 276

MVC Html Helper Rendering

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!

Upvotes: 0

Views: 668

Answers (2)

mat-mcloughlin
mat-mcloughlin

Reputation: 6722

Is this what your looking for?

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

Upvotes: 0

Martijn Laarman
Martijn Laarman

Reputation: 13536

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

Upvotes: 1

Related Questions