简体   繁体   English

在Windows MVC中将ViewData传递给ViewPage

[英]Passing ViewData to ViewPage in ASP.NET MVC

I'm trying to build a custom control in ASP.NET MVC. 我正在尝试在ASP.NET MVC中构建自定义控件。 The thing that I want to avoid is mixing the view (html) and the code (C#) in these controls. 我想避免的是在这些控件中混合视图(html)和代码(C#)。

After hours of googling and struggling with this problem I've finally thought of creating helper methods that use the ASP view Engine thought the ViewPage class. 经过几个小时的谷歌搜索和挣扎这个问题,我终于想到了创建使用ASP视图引擎认为ViewPage类的辅助方法。

I'm able to create an instance of the class and even load an ascx template using LoadControl and LoadTemplate methods but the problem is that when I pass a ViewData object to an instance of ViewPage class I cannot see the ViewData in the ViewPage and I'm getting this error: 我能够创建类的实例,甚至使用LoadControl和LoadTemplate方法加载ascx模板,但问题是当我将ViewData对象传递给ViewPage类的实例时,我无法在ViewPage中看到ViewData而我我收到此错误:

The ViewUserControl '~/Components/Controls/EditableLabel/Views/EditableLabel.ascx' cannot find an IViewDataContainer. ViewUserControl'〜/ Components / Controls / EditableLabel / Views / EditableLabel.ascx'找不到IViewDataContainer。 The ViewUserControl must be inside a ViewPage, ViewMasterPage, or another ViewUserControl. ViewUserControl必须位于ViewPage,ViewMasterPage或其他ViewUserControl中。

Generally the final effect that I want to achieve is loading controls with some parameters, these parameters should be readable in the template (ascx). 通常,我想要实现的最终效果是使用一些参数加载控件,这些参数应该在模板中可读(ascx)。

example: 例:

<% Html.EditableLabel(Writer, "memberName", "Name", "Antonio", "/identity/updateProperty", "memberName"); %>
  1. Is there a way to pass the ViewData to the ViewPage instance that I create? 有没有办法将ViewData传递给我创建的ViewPage实例?
  2. Is there a smarter way to create custom controls/widgets in ASP.NET MVC? 有没有更聪明的方法在ASP.NET MVC中创建自定义控件/小部件?

Thanks in advance 提前致谢

  1. You can pass VD to the VP from controller via "return View(...)" or via "ViewData[name] = value". 您可以通过“返回视图(...)”或“ViewData [name] = value”将VD传递给控制器​​的VP。
  2. It's better to use ascx controls which are inherited from ViewUserControl in order to create a custom control. 最好使用从ViewUserControl继承的ascx控件来创建自定义控件。 In this way you easily can pass data to it: 通过这种方式,您可以轻松地将数据传递给它:

     <% Html.RenderPartial("FooControl", new FooViewData(){ Name="aa", Type="awesome", Id = 2}); %> <% Html.RenderPartial("FooControl", new FooViewData(){ Name="aa", Type="awesome", Id = 2}, ViewData); %> 

And all necessary parameters you can define in your FooViewData class. 以及您可以在FooViewData类中定义的所有必要参数。

If you really want to write an extension to the HtmlHelper which will load some ascx controls you should look at a Repeater class in MvcFutures (sources of MVC are here ). 如果你真的想写一个HtmlHelper的扩展,它将加载一些ascx控件,你应该看看MvcFutures中的Repeater类(MVC的源代码在这里 )。 But I'm sure that in a common case you'll not need this. 但我敢肯定,在一般情况下你不需要这个。

I am tracking with zihotki on this one. 我正在跟踪zihotki这个。 With #2 I will add a caveat, however. 但是,对于#2,我会添加一个警告。 In the default implementation, which uses neutered ASPX and ASCX as views, creating MVC controls (use the template to ensure your dog is neutered) is the best option. 在默认实现中,使用neutered ASPX和ASCX作为视图,创建MVC控件(使用模板以确保您的狗被绝育)是最佳选择。

Now for the caveat. 现在请注意。 ASPX and ASCX do not have to be the views. ASPX和ASCX不一定是视图。 If you look at Phil Haack's blog, you will see some other view engines mentioned, like the Spark View Engine ( ASP.NET MVC Northwind Demo Using the Spark View Engine ). 如果你看一下Phil Haack的博客,你会看到一些其他的视图引擎,比如Spark View引擎( 使用Spark View引擎的ASP.NET MVC Northwind Demo )。 In fact, Phil has set up a sample of using alternative view engines in this entry: Rendering A Single View Using Multiple ViewEngines . 实际上,Phil在此条目中设置了使用替代视图引擎的示例: 使用多个ViewEngines渲染单个视图

I am not suggesting that you move to other view engines, only that there is an option, if you find one that better fits your problem domain. 我不是建议你转移到其他视图引擎,只是有一个选项,如果你找到一个更适合你的问题域。 Sometimes it is better to stick with the out of the box implementation. 有时最好坚持开箱即用的实现。 In this case, keep things simple with ASCX controls. 在这种情况下,使用ASCX控件保持简单。

By the way, if you are seriously getting into MVC as your UI story, you should look at the codeplex projects for MVC . 顺便说一下,如果你真的想要进入MVC作为你的UI故事, 你应该看看MVC的codeplex项目 The most important here is probably MVC.Contrib, but there are a few other gems in the list. 这里最重要的可能是MVC.Contrib,但列表中还有一些其他宝石。

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

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