需要帮助来提供更好的HtmlHelper扩展方法

时间:2021-03-31 19:03:51

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

继承了大多数部分输入视图的html

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

这是我的entension方法

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

提前致谢

3 个解决方案

#1


1  

Can you wrap all that logic into a single extension method?

你能将所有逻辑包装成一个扩展方法吗?

<%= Html.SmartDisplay("Id", (Model == null ? null : Model.Id))%> 

Then in the extension method, put all the code that checks whether it's displayable or readonly. This won't work if you don't have standard table layouts on all pages and for all controls, but if you do, it might work.

然后在扩展方法中,放入检查它是可显示还是只读的所有代码。如果您没有在所有页面和所有控件上使用标准表格布局,这将无效,但如果您这样做,它可能会起作用。

#2


0  

I would separate out the read-only and editable portions into 2 separate partial views. Leave it to your controller to decide which view should be rendered. You should not be making these kind of decisions in your views - they should be dumb.

我将只读和可编辑部分分成2个单独的部分视图。将它留给控制器来决定应该呈现哪个视图。你不应该在你的观点中做出这样的决定 - 他们应该是愚蠢的。

#3


0  

If you could create a cutom ViewModel class which has a collection of KeyValue pair where key = control name & Value = HtmlAttributes for that control, the controller might be able to set these attributes.

如果您可以创建一个cutom ViewModel类,该类具有KeyValue对的集合,其中key = control name&Value = HtmlAttributes用于该控件,则控制器可能能够设置这些属性。

On the View, you will directly bind the HtmlAttribute to the control.

在View上,您将直接将Ht​​mlAttribute绑定到控件。

Have not tried it out, but might work...

没有试过,但可能会工作......

HTH

EDIT: the KeyValue pair HtmlAttributes might be a collection of flags like IsEditable, IsVisible etc. which can be interpreted on the View via an extension method to render HtmlAttributes as needed so that we are not mixing the HTML part into the controller

编辑:KeyValue对HtmlAttributes可能是IsEditable,IsVisible等标志的集合,可以通过扩展方法在View上解释,以根据需要呈现HtmlAttributes,这样我们就不会将HTML部分混合到控制器中

#1


1  

Can you wrap all that logic into a single extension method?

你能将所有逻辑包装成一个扩展方法吗?

<%= Html.SmartDisplay("Id", (Model == null ? null : Model.Id))%> 

Then in the extension method, put all the code that checks whether it's displayable or readonly. This won't work if you don't have standard table layouts on all pages and for all controls, but if you do, it might work.

然后在扩展方法中,放入检查它是可显示还是只读的所有代码。如果您没有在所有页面和所有控件上使用标准表格布局,这将无效,但如果您这样做,它可能会起作用。

#2


0  

I would separate out the read-only and editable portions into 2 separate partial views. Leave it to your controller to decide which view should be rendered. You should not be making these kind of decisions in your views - they should be dumb.

我将只读和可编辑部分分成2个单独的部分视图。将它留给控制器来决定应该呈现哪个视图。你不应该在你的观点中做出这样的决定 - 他们应该是愚蠢的。

#3


0  

If you could create a cutom ViewModel class which has a collection of KeyValue pair where key = control name & Value = HtmlAttributes for that control, the controller might be able to set these attributes.

如果您可以创建一个cutom ViewModel类,该类具有KeyValue对的集合,其中key = control name&Value = HtmlAttributes用于该控件,则控制器可能能够设置这些属性。

On the View, you will directly bind the HtmlAttribute to the control.

在View上,您将直接将Ht​​mlAttribute绑定到控件。

Have not tried it out, but might work...

没有试过,但可能会工作......

HTH

EDIT: the KeyValue pair HtmlAttributes might be a collection of flags like IsEditable, IsVisible etc. which can be interpreted on the View via an extension method to render HtmlAttributes as needed so that we are not mixing the HTML part into the controller

编辑:KeyValue对HtmlAttributes可能是IsEditable,IsVisible等标志的集合,可以通过扩展方法在View上解释,以根据需要呈现HtmlAttributes,这样我们就不会将HTML部分混合到控制器中