English 中文(简体)
.aspx文件,其中包含ASP.NET MVC中的HTML和C#代码-这是正确的吗?
原标题:.aspx files with both HTML and C# code in ASP.NET MVC - is it correct?

因此,我决定尝试ASP.NET MVC,因为我之前几乎没有任何web开发经验。我在VisualStudio中创建了一个MVC 2.0项目,我看到其中包含了几个不同的.aspx页面。它们似乎混合了代码和HTML:

<% using (Html.BeginForm()) { %>
    <%: Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.") %>
    <div>
        <fieldset>
            <legend>Account Information</legend>

            <div class="editor-label">
                <%: Html.LabelFor(m => m.UserName) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(m => m.UserName) %>
                <%: Html.ValidationMessageFor(m => m.UserName) %>
            </div>
            ...

这是标准做法吗?还是应该将HTML和代码完全分离?

谢谢

最佳回答

这绝对是标准做法。从视图中完全废除编程逻辑(甚至表示逻辑)的旧概念是历史的残余。

问题回答

是的,这是ASP.NET MVC视图的常见做法。

这是ASP.NET MVC中的标准配置。这段代码特别是HTML帮助器扩展,应该像那样内联放置在标记中。它们的具体用途是根据您使用它们的方式来呈现标记。

请注意,这种代码/标记组合有点笨拙的想法是ASP.NETMVC的Razor视图引擎,它在HTML标记中更清晰、更具表现力。

这是标准做法。MVC项目使用视图、模型和控制器。必须将一些代码放入视图中才能处理动态内容的输出。与网络表单不同,它们不使用代码隐藏。视图中只应显示代码。更复杂的处理应该在模型或控制器中。

对只需确保代码集中在基于Model创建View(html)上即可。如果您在这里有代码进入存储库以获取数据,那么该代码应该位于控制器中,并用视图需要显示的数据填充模型。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签