简体   繁体   English

ASP.NET:在Web表单中使用MVC部分视图

[英]ASP.NET: Using MVC partial view in web forms

I've got a regular ASP.NET web form page and am pasting in a <%= Html.Partial(...) %> that I took from an MVC view page. 我有一个常规的ASP.NET Web表单页面,并粘贴了从MVC视图页面<%= Html.Partial(...) %><%= Html.Partial(...) %>

What do I need to do to this web forms .aspx to get the Html.Partial to work? 我需要对此Web窗体.aspx进行什么操作才能使Html.Partial工作? I've put this at the top of my web forms .aspx page: 我将其放在Web表单.aspx页面的顶部:

<%@ Import Namespace="System.Web.Mvc" %>
<%@ Import Namespace="System.Web.Mvc.Html" %>

But I'm still getting error: 但是我仍然遇到错误:

Compiler Error Message: CS0103: The name 'Html' does not exist in the current context

You are getting the error because Html is a property of the System.Web.Mvc.ViewPage class and is an instance of the HtmlHelper class. 由于HtmlSystem.Web.Mvc.ViewPage类的属性,并且是HtmlHelper类的实例,因此出现错误。 The System.Web.Mvc.ViewPage class is the class that your default ASP.NET MVC views inherit from, so you have access to the Html property in your views. System.Web.Mvc.ViewPage类是默认ASP.NET MVC视图所继承的类,因此您可以在视图中访问Html属性。

I don't know how you would go about creating an HtmlHelper instance in your webforms page but I would imagine that you'd have a hard time because it's constructor takes a ViewContext and an IViewDataContainer . 我不知道您将如何在Webforms页面中创建HtmlHelper实例,但是我想您会遇到IViewDataContainer因为它的构造函数采用了ViewContextIViewDataContainer

这是有关如何在Webforms中使用MVC的文章: http : //www.packtpub.com/article/mixing-asp.net-webforms-and-asp.net-mvc

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

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