简体   繁体   English

在解决方案中的项目之间共享PartialView?

[英]Share PartialView between projects in a solution?

I am developing 2 MVC5 websites. 我正在开发2个MVC5网站。 They run seperately, but actually related to each other (1 Admin site, and 1 User site). 它们单独运行,但实际上彼此相关(1个管理站点和1个用户站点)。 In both site, I have to render a graphical board using HTML table. 在这两个站点中,我必须使用HTML表格渲染图形板。 I am using PartialView with a Model to render it. 我使用PartialView与模型来渲染它。

Currently, this is my solution structure: 目前,这是我的解决方案结构:

  • Project.Common (A class library project, contain the model) Project.Common(一个类库项目,包含模型)

  • Project.Admin (MVC5 project for the Admin site), refering to Project.Common. Project.Admin(管理站点的MVC5项目),参考Project.Common。

  • Project.User (MVC5 project for User site), refering to Project.Common. Project.User(用户站点的MVC5项目),参考Project.Common。

To render the graphical board, I will use BoardData class from Project.Common, which is ok. 要渲染图形板,我将使用Project.Common中的BoardData类,这没关系。 But with current solution structure, I have to create 2 PartialView in each MVC5 project, which may be hard to maintain later. 但是使用当前的解决方案结构,我必须在每个MVC5项目中创建2个PartialView,这可能很难在以后维护。

Please suggest a good solution for reusing the PartialView. 请为重新使用PartialView建议一个很好的解决方案。 I already know I can write Helper, but that way, I have to write HTML code inside C# code, which is not very good. 我已经知道我可以编写Helper了,但是这样,我必须在C#代码中编写HTML代码,这不是很好。 Is there any way to do this within Razor View? 在Razor View中有没有办法做到这一点?

One of the options you can use is add a link to a file. 您可以使用的一个选项是添加文件的链接。 In this case keep your Partial view in just single project and in second project use Add -> Existing Item , select your partial view from the first project and click on a small down arrow next to the Add button and choose Add as Link . 在这种情况下,将部分视图保留在单个项目中,在第二个项目中使用Add -> Existing Item ,从第一个项目中选择部分视图,然后单击Add按钮旁边的小向下箭头,然后选择Add as Link More info can be found on MSDN , see section Add an existing item as a link. 可以在MSDN上找到更多信息,请参阅添加现有项目作为链接部分。

In this case you will have a partial view only in one project and any changes to it will automatically be applied to other project as well. 在这种情况下,您将只在一个项目中具有部分视图,并且对其的任何更改也将自动应用于其他项目。

您应该将您的视图嵌入到Project.Common dll中,详细说明可以在https://visualstudiogallery.msdn.microsoft.com/f28290ce-d987-4f91-b034-707031e10ce6上找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM