启用对呈现的HTML的完全控制 [英] Enables the full control over the rendered HTML

查看:100
本文介绍了启用对呈现的HTML的完全控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我正在学习MVC,我发现MVC已经完全控制了渲染的HTML。



现在我们在MVC3中使用HTML Helpers,其中 Html.EditorFor 代表HTML < input> 元素。

现在我不明白如果我们使用这个帮助器那么mvc如何完全控制渲染的html?

如果我们在asp.net中使用gridview或其他一些控件,那么它们会自动创建table,tr,td结构。 Html助手也发生了同样的事情。



那么任何人都可以解释一下它是什么意思吗?

Hello,

I am learning MVC and i had find that MVC has full control over the rendered HTML.

Now we are using HTML Helpers in MVC3, where Html.EditorFor stands for HTML <input> elements.
Now i am not understand that if we use this helpers then how mvc has full control over rendered html?
If we use gridview or some other controls in asp.net then they create table, tr, td structure automatically. Same thing happening with Html helpers.

So can any one please explain me what it mean?

推荐答案

继续使用文本框的示例。



Html.TextBoxFor(m => m.Username)创建
To go with your example of using a textbox.

Html.TextBoxFor(m=>m.Username) creates the stub of
<input type="text" name="Username" id="Username" />





为了在MVC 3中操作此输入(这几乎是html属性)是通过在适用的参数中添加属性



所以这个





In order to manipulate this input in MVC 3 (which is pretty much html attributes) is by adding attributes into the applicable parameters

So This

<input type="text" name="Username" id="Username" class="textbox" /> 





在MVC中变为





In MVC becomes

@Html.TextBoxFor(m=>m.Username, new { @class = "textbox" });







所以对于下拉列表,看起来像下面的HTML






So for a drop down that looks like the following HTML

<select name="States" id="States" class="dropdown">
<option>FL</option>
<option>NC</option>
</select>





在MVC中成为现实





Becomes this in MVC

SelectListItem[] listStates = new[] {
    new SelectListItem() { Text = "FL"},
    new SelectListItem() { Text = "NC"}
};

@Html.DropDownListFor(m => m.States, listStates, new { @class = "dropdown" })





要指定默认选择的内容,您需要做的就是





To specify something to be selected by default all you would have to do is

SelectListItem[] listStates = new[] {
    new SelectListItem() { Text = "FL"},
    new SelectListItem() { Text = "NC", Selected = true}
};





将所选项设置为true,该下拉选项将成为所选值。



所以你看,它不是MVC不能让你完全控制html,它的MVC创造了一种将html视为对象的方法,而不是。



一些链接有助于建立/澄清我简要解释的内容。



实用的ASP.NET MVC(3)提示[ ^ ]



ASP.NET MVC3 Razor with jQuery For Beginners [ ^ ]



http://www.asp.net/mvc / mvc3 [ ^ ]



html助手的细节



http://stephenwalther.com/archive/2009/03/03/chapter-6-understanding-html-helpers.aspx [ ^ ]



http://weblogs.asp.net/scottgu/archive /2010/01/10/asp-net-mvc-2-strongly-typed-html-helpers.aspx [ ^ ]



Set Selected to true and that drop down option would become the selected value.

So you see, its not that MVC doesnt give you full control over the html, its that MVC creates a way to think of the html as objects and what not.

Some links that will help build/clarify on what i''ve briefly explained.

Practical ASP.NET MVC (3) tips[^]

ASP.NET MVC3 Razor With jQuery For Beginners[^]

http://www.asp.net/mvc/mvc3[^]

Specifics on html helpers

http://stephenwalther.com/archive/2009/03/03/chapter-6-understanding-html-helpers.aspx[^]

http://weblogs.asp.net/scottgu/archive/2010/01/10/asp-net-mvc-2-strongly-typed-html-helpers.aspx[^]


你有完全的控制权,因为你不依赖于任何人。 html帮助器将呈现纯html元素,其他一切都取决于您。这些帮助程序有许多重载来帮助您发出所需的代码。如果缺少某些东西,你可以使用几行代码制作自己的重载或帮助。

顺便说一句,如果你不想使用帮助者,那么就不要使用它们,用php编写你的元素。但是你错过了重点:帮助器将使你的应用程序更容易调试,维护和开发 - 你不必放弃控制。



什么你错过了控制吗?
You have full control, since you don''t depend on anybody. The html helpers will render pure html elements, everything else is up to you. Those helpers have many overloads to help you emit the code you need. If something is missing, you can make your own overload or helper with a few lines of code.
Btw, if you don''t want to use the helpers, than don''t use them, write your elements by hand as with php. But than you miss the point: the helper will make your application easier to debug, maintain and develop - and you won''t have to give up control.

What control are you missing?


这篇关于启用对呈现的HTML的完全控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆