问题描述
ASP.NET MVC中服务器控件的替代品是什么?我想做的是创建一个声明式和命令式绑定,以便我可以编写
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
<cc1:MyControl Header="Some Header" Content="Some Content" />
这意味着将创建MyControl类的实例,并可能将其呈现给
which would mean that an instance of the MyControl class will be created and possibly rendered to
<h1>Some Header</h1>
<p>Content</p>
我不希望任何视图状态或回发废话,而只是模块化.我也希望这些模块包含在一个单独的类库中,所以ViewUserControls不会为我做.以正常方式使用服务器控件是可行的,但是它会生成一个表单标签和一个viewstate字段,如果可以避免的话,我将不希望这样做.
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.
我已经看到这个问题和有关如何在ASP.NET MVC中使用服务器控件,但它们没有提供足够的答案.
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.
编辑:我找到了答案.当我使用设计器添加用户控件时,它会自动创建一个< form>表单.我错过了.如果我只是删除该标签,一切将正常运行.
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.
推荐答案
如果ASP.NET MVC中不需要使用服务器形式的呈现,则仍可以使用所有控件.
You can still use all controls in ASP.NET MVC if they don't require rendering in a server form.
ascx
文件和@Register
指令仍然运行良好.伟大的新事物是Html.RenderPartial
方法,它使您可以将模型对象传递到局部视图(ascx
)并进行相应渲染.
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.
这篇关于MVC中服务器控件的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!