.aspx files with both HTML and C# code in ASP.NET MVC - is it correct?
So I've decided to try out ASP.NET MVC, having almost no prior experience in any kind of web developm开发者_如何学运维ent. I've created a MVC 2.0 project in Visual Studio and I see there are a couple of different .aspx pages included. They seem to mix both code and 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>
...
Is this standard practice? Or should I totally separate HTML and code?
Thanks
This is absolutely standard practice. That old notion of completely abolishing programming logic (even presentation logic) from views is a residue of history.
Yes that is common practice with ASP.NET MVC views.
This is standard in ASP .NET MVC. This code specifically is HTML helper extensions which are supposed to be placed inline in the markup like that. Their specific purpose is to render markup based on how you use them.
Note that the idea that this code/markup mix is a bit unwieldy is a driving force behind the Razor view engine for ASP .NET MVC, which is a little cleaner and more expressive within the HTML markup.
It is standard practice. MVC projects uses views, models and controllers. Some code does have to be put into the view to handle the output of dynamic content. Unlike webforms, they do not use a codebehind. Only display code should be in the view. More complex processing should be in the model or the controller.
Yes. Just make sure that the code is all centered around creating a View
(html) based on your Model
. If you have code in here that goes to a repository to get data out, that code should reside in the Controller, and populate the Model with the data that your View needs to display.
精彩评论