Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使这两块几乎相同的代码可重用?_C#_Asp.net Mvc_Partial Views_Abstraction_Code Reuse - Fatal编程技术网

C# 如何使这两块几乎相同的代码可重用?

C# 如何使这两块几乎相同的代码可重用?,c#,asp.net-mvc,partial-views,abstraction,code-reuse,C#,Asp.net Mvc,Partial Views,Abstraction,Code Reuse,我需要关于如何使以下两个代码块可重用的建议。我必须生成另一个基金表,在此过程中,我想创建FundsTable.ascx部分视图,所有需要显示基金表的视图都可以使用该视图 // Inherits="System.Web.Mvc.ViewPage<CompanyViewModel> // this is a company <%foreach (var fund in Model.PageFunds){%> <% foreach (var shareClass

我需要关于如何使以下两个代码块可重用的建议。我必须生成另一个基金表,在此过程中,我想创建FundsTable.ascx部分视图,所有需要显示基金表的视图都可以使用该视图

// Inherits="System.Web.Mvc.ViewPage<CompanyViewModel> // this is a company
<%foreach (var fund in Model.PageFunds){%>

    <% foreach (var shareClass in fund.ShareClasses) {%>

        <tr class="shareclass">
            <td>
                // displays an image if the ViewModel's Company is not unlisted
                <%= Html.TearsheetImage((Model.Company.ListingType != ListingType.UNLISTED))%>
            </td>
        </tr>
<% } }%>
//Inherits=“System.Web.Mvc.ViewPage//这是一家公司
//如果ViewModel的公司未列出,则显示图像

//Inherits=“System.Web.Mvc.ViewPage//这是一组公司
//如果ViewModel中有任何公司,则显示图像
//该列表并非未列出
我想我需要把不同之处抽象出来,但我不知道该放在哪里。这里有什么我应该遵循的经验法则吗

CompanyViewModel
GroupViewModel
是否都应该实现一个接口来决定项目是否未列出?另外,我的FundTable.ascx应该是什么类型?我想
CompanyViewModel
GroupViewModel
都可以扩展
FundViewModel
(或者别的什么)我可以让FundTable成为一个
ViewUserControl
,但我认为这行不通,因为决定是否显示图像所需的功能需要独立地来自
CompanyViewModel
&
GroupViewModel


另外,我越想这个,我就越狡猾!有什么想法或建议吗?谢谢

如果我读对了这篇文章,那么代码的不同之处在于您决定是否显示图像

如果这是正确的,这是一个完美的地方做一点函数编程

为.ascx创建视图模型。我们称之为基金稳定

它将有两个属性:

Func<PageFund,bool> ShowImage {get;set;}
IEnumerable PageFund Funds {get;set;}
Func ShowImage{get;set;}
IEnumerable PageFund基金{get;set;}
将FundsTable.ascx强类型化到此对象

现在可以传入您关于是否显示的逻辑:

FundsTable ft = new FundsTable();
ft.ShowImage = f => f.SomeCombinationOfLogic == SomeOtherThing; //<-- Your function can be anything that returns a bool
FundsTable ft=new FundsTable();

ft.ShowImage=f=>f.SomeCombinationOfLogic==SomeOtherThing// 对我来说,这样一个问题的根源在于,公司视图模型和公司组视图模型本质上是相同的(或应该是相同的),而您不知何故(可能是无意中)成功地从中创建了两个不同的视图模型。通常的逻辑是,一组公司应该只是一个
列表
。您可以向视图或viewusercontrol发送列表。您不必为它创建一个新的viewmodel类,只需将集合作为属性包含在该类中即可


查看您在评论的两行中所写的内容并思考。

这看起来很有趣。但我有点不确定如何实现它。我必须有一个在Group视图和Company视图上呈现Partial的指令,类似于:
,但这必须能够包含区分它们所需的逻辑。我该怎么做呢?在进行渲染之前,请为FundTable部分创建模型并将其传入:
YourLogicHere,Funds=model.PageFunds};%%>谢谢,老兄,成功了。这正是我们在这个项目中需要的。我现在在两个地方使用FundsTable,第三个设置将在明天实施。我还可以在整个解决方案中对其进行全面的修改。我们将使这个项目成形!
FundsTable ft = new FundsTable();
ft.ShowImage = f => f.SomeCombinationOfLogic == SomeOtherThing; //<-- Your function can be anything that returns a bool
<% foreach (var shareClass in fund.ShareClasses) {%>

    <tr class="shareclass">
        <td>
            // displays an image if any Company in the ViewModel's 
            // List<Company> is not unlisted
           <% if(Model.ShowImage(fund)) {%>
               <%= Html.TearsheetImage(fund)%>
           <% } %>
        </td>
    </tr>