What is the replacement for a server control in ASP.NET MVC? What I want to do is to create a declarative and imperative binding so I can write
ASP.NET MVC中服务器控件的替代品是什么?我想要做的是创建一个声明性和命令式绑定,以便我可以编写
<cc1:MyControl Header="Some Header" Content="Some Content" />
which would mean that an instance of the MyControl class will be created and possibly rendered to
这意味着将创建一个MyControl类的实例并可能将其呈现
<h1>Some Header</h1>
<p>Content</p>
I don't want any viewstate or postback crap, just the modularity. I also want these modules to be contained in a separate class library, so ViewUserControls will not do for me. Using a server controls in the normal way works, but it generates a form tag and a viewstate field, which I do not want if I can avoid it.
我不想要任何viewstate或postback crap,只需要模块化。我还希望这些模块包含在一个单独的类库中,因此ViewUserControls不会对我有用。以正常方式使用服务器控件可以正常工作,但它会生成一个表单标记和一个viewstate字段,如果我可以避免它,我不想要它。
I have seen this question and this one about how to use server controls in ASP.NET MVC, but they do not provide enough answer.
我已经看到了这个问题,这个问题关于如何在ASP.NET MVC中使用服务器控件,但它们没有提供足够的答案。
Edit: I found the answer. When I added the user control using the designer, it automatically created a <form> which I missed. If I simply remove that tag, everything works perfectly.
编辑:我找到了答案。当我使用设计器添加用户控件时,它会自动创建一个我错过的
3 个解决方案
#1
You can still use all controls in ASP.NET MVC if they don't require rendering in a server form.
如果不需要以服务器形式呈现,您仍然可以使用ASP.NET MVC中的所有控件。
ascx
files and @Register
directives still work pretty well. The great new thing is Html.RenderPartial
method that lets you pass a model object to a partial view (ascx
) and have it render accordingly.
ascx文件和@Register指令仍然可以很好地工作。最新的东西是Html.RenderPartial方法,它允许您将模型对象传递给局部视图(ascx)并相应地进行渲染。
#2
Just adding one more possibility to Mehrdad answer, you can use extension methods to do a simple control like this:
只需为Mehrdad答案添加一个可能性,您可以使用扩展方法来执行这样的简单控制:
<%= html.MyControl( "Some header", "Some content" ) %>
<Extension()> _
Public Function MyControl(ByVal htmlHelper As HtmlHelper, _
ByVal Header As String, _
ByVal Content As String) As String
Dim sb As New StringBuilder()
sb.AppendFormat("<h1>{0}</h1>", Header)
sb.AppendFormat("<p>{0}</p>", Content)
Return sb.ToString()
End Function
Or you can make a more complex control like this example: Create an ASP.NET MVC GridView Helper Method
或者您可以像这个示例一样进行更复杂的控制:创建一个ASP.NET MVC GridView Helper方法
#3
Other than the controls which still work with ASP.Net MVC, you can use mvc
controls.
除了仍然可以与ASP.Net MVC一起使用的控件之外,您还可以使用mvc控件。
- Repeater example - dead link
- Exploring ASP.Net MVC Futures - dead link
转发器示例 - 死链接
探索ASP.Net MVC期货 - 死链接
UPDATE: This answer was for ASP.Net MVC 1.0 in 2009. It is outdated and irrelevant at this point.
更新:这个答案是2009年的ASP.Net MVC 1.0。它已经过时且无关紧要。
#1
You can still use all controls in ASP.NET MVC if they don't require rendering in a server form.
如果不需要以服务器形式呈现,您仍然可以使用ASP.NET MVC中的所有控件。
ascx
files and @Register
directives still work pretty well. The great new thing is Html.RenderPartial
method that lets you pass a model object to a partial view (ascx
) and have it render accordingly.
ascx文件和@Register指令仍然可以很好地工作。最新的东西是Html.RenderPartial方法,它允许您将模型对象传递给局部视图(ascx)并相应地进行渲染。
#2
Just adding one more possibility to Mehrdad answer, you can use extension methods to do a simple control like this:
只需为Mehrdad答案添加一个可能性,您可以使用扩展方法来执行这样的简单控制:
<%= html.MyControl( "Some header", "Some content" ) %>
<Extension()> _
Public Function MyControl(ByVal htmlHelper As HtmlHelper, _
ByVal Header As String, _
ByVal Content As String) As String
Dim sb As New StringBuilder()
sb.AppendFormat("<h1>{0}</h1>", Header)
sb.AppendFormat("<p>{0}</p>", Content)
Return sb.ToString()
End Function
Or you can make a more complex control like this example: Create an ASP.NET MVC GridView Helper Method
或者您可以像这个示例一样进行更复杂的控制:创建一个ASP.NET MVC GridView Helper方法
#3
Other than the controls which still work with ASP.Net MVC, you can use mvc
controls.
除了仍然可以与ASP.Net MVC一起使用的控件之外,您还可以使用mvc控件。
- Repeater example - dead link
- Exploring ASP.Net MVC Futures - dead link
转发器示例 - 死链接
探索ASP.Net MVC期货 - 死链接
UPDATE: This answer was for ASP.Net MVC 1.0 in 2009. It is outdated and irrelevant at this point.
更新:这个答案是2009年的ASP.Net MVC 1.0。它已经过时且无关紧要。