Need help coming up with a better HtmlHelper Extension method.
Posted
by zSysop
on Stack Overflow
See other posts from Stack Overflow
or by zSysop
Published on 2010-02-10T17:40:29Z
Indexed on
2010/04/08
4:53 UTC
Read the original article
Hit count: 308
htmlhelpers
|asp.net-mvc
Hi all,
I've inherited the following code and i was wondering if i could pick at your brains to see if there's a nicer way to do duplicate this.
Heres the html for most of our partial input views
<% if (Html.IsInputReadOnly()) { %>
<td>
Id
</td>
<td>
<%= Html.TextBox(
"Id"
, (Model == null ? null : Model.Id)
, new { @readonly = "readonly", @disabled="disabled" }
)%>
<% } elseif (Html.IsInputDisplayable() == false) { %>
<td></td>
<td></td>
<% } else { %>
<td>Id</td>
<td><%= Html.TextBox("Id")%>
<%= Html.ValidationMessage("Id", "*")%>
</td>
<%} %>
Here are my entension methods
public static bool IsInputReadOnly(this HtmlHelper helper)
{
string actionName = ActionName(helper);
// The Textbox should be read only on all pages except for the lookup page
if (actionName.ToUpper().CompareTo("EDIT") == 0)
return true;
return false;
}
public static bool IsInputDisplayable(this HtmlHelper helper)
{
string actionName = ActionName(helper);
// The Textbox should be read only on all pages except for the lookup page
if (actionName.ToUpper().CompareTo("CREATE") == 0)
return true;
return false;
}
Thanks in advance
© Stack Overflow or respective owner