What You Have to Know about ASP.net MVC
2012-09-16 12:32
597 查看
Controller
ASP.NET MVC invokes different controller classes (and different action methods within them) depending on the incoming URL. The default mapping logic used by ASP.NET MVC uses a format like this to determine what code to invoke:
The method named
If We have an Action in Controller like this.
The example URL (http://localhost:xxxx/HelloWorld/Welcome?name=Scott&numtimes=4). You can try different values for
You'll create a view template file using the new Razor view engine introduced with ASP.NET MVC 3. Razor-based view templates have a .cshtml file extension, and provide an elegant way to create HTML output using C#. Razor minimizes the number of characters and keystrokes required when writing a view template, and enables a fast, fluid coding workflow.
(See also : http://msdn.microsoft.com/en-us/vs2010trainingcourse_aspnetmvc3razor_topic2 Razor)
(See also : http://www.asp.net/mvc/videos tutorial video about Razor)
(See also: http://rachelappel.com/razor/introducing-mvc-development-w-the-razor-view-engine-for-visual-studio-developers/ Razor)
(See also: http://midnightprogrammer.net/post/RAZOR-View-Engine-In-ASPNET-MVC-3.aspx Razor)
Layout Templetes
You can create multiple Layout pages and use it in differnt views. You may mention the new layout pages inside your views by mentioning the Layout property in your views.
Create a new
And if you want to use this Layout in a view, you can do it like this
If you want to use the new Layout for all the Views by default without explicitly mentioning like the above code, you can do it in the
Passing data between view and controller
1.You can do this by having the controller put the dynamic data that the view template needs in a
You saw how a controller can pass data or objects to a view template using the
The ASP.NET framework model binder takes the posted form values and creates a
Remember , even there is no method decorated with HttpPost attribute . the submit action will be directed to the default HttpGet Method.
see the code debugger fellowing. the HttpPost method is commented.
So even you add post form when creating your searchIndex view like this.
And there's no
Entity Framework code first migration and DB initialize
Wire this class up each time when app start.
Validation in UI
The first action method displays the initial Create form. The second handles the form post. The second
Below is the Create.cshtml view template that you scaffolded earlier in the tutorial. It's used by the action methods shown above both to display the initial form and to redisplay it in the event of an error.
Notice how the code uses an
The validation rules and the error strings are specified only in the
See also:
/article/5005764.html
/article/5075536.html
http://www.asp.net/mvc/tutorials/older-versions/views/creating-custom-html-helpers-cs
/article/3627100.html(html helper)
http://msdn.microsoft.com/en-us/library/dd394711.aspx(passing data and validate message)
http://msdn.microsoft.com/en-us/library/ee256141.aspx (validate)
http://www.asp.net/mvc/tutorials/views/introduction-to-razor-syntax (razor syntax)
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx(mvc route tools)
/article/6924153.html(map route)
/article/4622413.html(filter)
http://weblogs.asp.net/scottgu/archive/2010/10/19/asp-net-mvc-3-new-model-directive-support-in-razor.aspx(@model keyword in Razor)
http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx(layout.cshtml, @url.content)
http://weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx(Layouts and Sections with Razor)
http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx
ASP.NET MVC invokes different controller classes (and different action methods within them) depending on the incoming URL. The default mapping logic used by ASP.NET MVC uses a format like this to determine what code to invoke:
/[Controller]/[ActionName]/[Parameters]
The method named
Indexis the default method that will be called on a controller if URL is not explicitly specified Action Name.
If We have an Action in Controller like this.
public string Welcome(string name, int numTimes = 1) { return HttpUtility.HtmlEncode("Hello " + name + ", NumTimes is: " + numTimes); }
The example URL (http://localhost:xxxx/HelloWorld/Welcome?name=Scott&numtimes=4). You can try different values for
nameand
numtimesin the URL. The system automatically maps the named parameters from the query string in the address bar to parameters in your method.
You'll create a view template file using the new Razor view engine introduced with ASP.NET MVC 3. Razor-based view templates have a .cshtml file extension, and provide an elegant way to create HTML output using C#. Razor minimizes the number of characters and keystrokes required when writing a view template, and enables a fast, fluid coding workflow.
(See also : http://msdn.microsoft.com/en-us/vs2010trainingcourse_aspnetmvc3razor_topic2 Razor)
(See also : http://www.asp.net/mvc/videos tutorial video about Razor)
(See also: http://rachelappel.com/razor/introducing-mvc-development-w-the-razor-view-engine-for-visual-studio-developers/ Razor)
(See also: http://midnightprogrammer.net/post/RAZOR-View-Engine-In-ASPNET-MVC-3.aspx Razor)
Layout Templetes
You can create multiple Layout pages and use it in differnt views. You may mention the new layout pages inside your views by mentioning the Layout property in your views.
Create a new
Viewand name it as
_AdminLayout.cshtmlunder
Views/Sharedfolder. You may create your HTML markup for your master page template there. Make sure you have
@RenderBody()section in your Layout page so that the content of your normal views will be replaced here when it renders.
<html> <head> </head> <body> <h1>MynewLayout</h1> @RenderBody() </body> </html>
And if you want to use this Layout in a view, you can do it like this
@{ Layout="~/Views/Shared/_AdminLayout.cshtml"; } <p>The content here will be present inside the render body section of layout</p>
If you want to use the new Layout for all the Views by default without explicitly mentioning like the above code, you can do it in the
Views/_ViewStart.cshtmlfile
@{ Layout="~/Views/Shared/_AdminLayout.cshtml"; }
Passing data between view and controller
1.You can do this by having the controller put the dynamic data that the view template needs in a
ViewBagobject that the view template can then access.
@{ ViewBag.Title = "Welcome"; } <h2>Welcome</h2> <ul> @for (int i=0; i < ViewBag.NumTimes; i++) { <li>@ViewBag.Message</li> } </ul>
You saw how a controller can pass data or objects to a view template using the
ViewBagobject. The
ViewBagis a dynamic object that provides a convenient late-bound way to pass information to a view.
Processing the POST Request
The following listing shows theHttpPostversion of the
Editaction method.
[HttpPost] public ActionResult Edit(Movie movie) { if (ModelState.IsValid) { db.Entry(movie).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(movie); }
The ASP.NET framework model binder takes the posted form values and creates a
Movieobject (this is done automatically) that's passed as the m
ovieparameter. The
ModelState.IsValidcheck in the code verifies that the data submitted in the form can be used to modify a
Movieobject. If the data is valid, the code saves the movie data to the
Moviescollection of the
MovieDBContextinstance. The code then saves the new movie data to the database by calling the
SaveChangesmethod of
MovieDBContext, which persists changes to the database. After saving the data, the code redirects the user to the
Indexaction method of the
MoviesControllerclass, which causes the updated movie to be displayed in the listing of movies.
Remember , even there is no method decorated with HttpPost attribute . the submit action will be directed to the default HttpGet Method.
see the code debugger fellowing. the HttpPost method is commented.
So even you add post form when creating your searchIndex view like this.
@model IEnumerable<TestMvc4App.Models.Movie> @{ ViewBag.Title = "SearchIndex"; } <h2>SearchIndex</h2> <p> @Html.ActionLink("Create New", "Create") @using (Html.BeginForm()){ <p> Title: @Html.TextBox("SearchString") <input type="submit" value="Filter" /></p> } </p> ....
And there's no
HttpPostoverload of the
SearchIndexmethod. You don't need it, because the method isn't changing the state of the application, just filtering data.
public ActionResult SearchIndex(string searchString) { var movies = from m in db.Movies select m; if (!String.IsNullOrEmpty(searchString)) { movies = movies.Where(s => s.Title.Contains(searchString)); } return View(movies); }
Entity Framework code first migration and DB initialize
using System; using System.Collections.Generic; using System.Data.Entity; namespace MvcMovie.Models { public class MovieInitializer : DropCreateDatabaseIfModelChanges<MovieDBContext> { protected override void Seed(MovieDBContext context) { var movies = new List<Movie> { new Movie { Title = "When Harry Met Sally", ReleaseDate=DateTime.Parse("1989-1-11"), Genre="Romantic Comedy", Rating="R", Price=7.99M}, new Movie { Title = "Ghostbusters ", ReleaseDate=DateTime.Parse("1984-3-13"), Genre="Comedy", Rating="R", Price=8.99M}, new Movie { Title = "Ghostbusters 2", ReleaseDate=DateTime.Parse("1986-2-23"), Genre="Comedy", Rating="R", Price=9.99M}, new Movie { Title = "Rio Bravo", ReleaseDate=DateTime.Parse("1959-4-15"), Genre="Western", Rating="R", Price=3.99M}, }; movies.ForEach(d => context.Movies.Add(d)); } } }
Wire this class up each time when app start.
protected void Application_Start() { Database.SetInitializer<MovieDBContext>(new MovieInitializer()); AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); }
Validation in UI
[HttpPost] public ActionResult Create(Movie movie) { if (ModelState.IsValid) { db.Movies.Add(movie); db.SaveChanges(); return RedirectToAction("Index"); } return View(movie); }
The first action method displays the initial Create form. The second handles the form post. The second
Createmethod calls
ModelState.IsValidto check whether the movie has any validation errors. Calling this method evaluates any validation attributes that have been applied to the object. If the object has validation errors, the
Createmethod redisplays the form. If there are no errors, the method saves the new movie in the database.
Below is the Create.cshtml view template that you scaffolded earlier in the tutorial. It's used by the action methods shown above both to display the initial form and to redisplay it in the event of an error.
@model MvcMovie.Models.Movie @{ ViewBag.Title = "Create"; } <h2> Create</h2> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>Movie</legend> <div class="editor-label"> @Html.LabelFor(model => model.Title) </div> <div class="editor-field"> @Html.EditorFor(model => model.Title) @Html.ValidationMessageFor(model => model.Title) </div> <div class="editor-label"> @Html.LabelFor(model => model.ReleaseDate) </div> <div class="editor-field"> @Html.EditorFor(model => model.ReleaseDate) @Html.ValidationMessageFor(model => model.ReleaseDate) </div> <div class="editor-label"> @Html.LabelFor(model => model.Genre) </div> <div class="editor-field"> @Html.EditorFor(model => model.Genre) @Html.ValidationMessageFor(model => model.Genre) </div> <div class="editor-label"> @Html.LabelFor(model => model.Price) </div> <div class="editor-field"> @Html.EditorFor(model => model.Price) @Html.ValidationMessageFor(model => model.Price) </div> <div class="editor-label"> @Html.LabelFor(model => model.Rating) </div> <div class="editor-field"> @Html.EditorFor(model => model.Rating) @Html.ValidationMessageFor(model => model.Rating) </div> <p> <input type="submit" value="Create" /> </p> </fieldset> } <div> @Html.ActionLink("Back to List", "Index") </div>
Notice how the code uses an
Html.EditorForhelper to output the
<input>element for each
Movieproperty. Next to this helper is a call to the
Html.ValidationMessageForhelper method. These two helper methods work with the model object that's passed by the controller to the view (in this case, a
Movieobject). They automatically look for validation attributes specified on the model and display error messages as appropriate.
The validation rules and the error strings are specified only in the
Movieclass.
using System; using System.Data.Entity; using System.ComponentModel.DataAnnotations; namespace MvcMovie.Models { public class Movie { public int ID { get; set; } [Required(ErrorMessage = "Title is required")] public string Title { get; set; } [Required(ErrorMessage = "Date is required")] public DateTime ReleaseDate { get; set; } [Required(ErrorMessage = "Genre must be specified")] public string Genre { get; set; } [Required(ErrorMessage = "Price Required")] [Range(1, 100, ErrorMessage = "Price must be between $1 and $100")] public decimal Price { get; set; } [StringLength(5)] public string Rating { get; set; } } public class MovieDBContext : DbContext { public DbSet<Movie> Movies { get; set; } } }
See also:
/article/5005764.html
/article/5075536.html
http://www.asp.net/mvc/tutorials/older-versions/views/creating-custom-html-helpers-cs
/article/3627100.html(html helper)
http://msdn.microsoft.com/en-us/library/dd394711.aspx(passing data and validate message)
http://msdn.microsoft.com/en-us/library/ee256141.aspx (validate)
http://www.asp.net/mvc/tutorials/views/introduction-to-razor-syntax (razor syntax)
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx(mvc route tools)
/article/6924153.html(map route)
/article/4622413.html(filter)
http://weblogs.asp.net/scottgu/archive/2010/10/19/asp-net-mvc-3-new-model-directive-support-in-razor.aspx(@model keyword in Razor)
http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx(layout.cshtml, @url.content)
http://weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx(Layouts and Sections with Razor)
http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx
相关文章推荐
- What ASP.NET Developers Should Know About JavaScript
- ASP.NET MVC: Do You Know Where Your TempData Is?
- What is LineageOS ? Everything you need to know about Lineage OS
- Register ASP.NET 4.0 An error has occurred: 0x8007b799 You must have administrative rights on this machine in order to ru
- What ASP.NET Developers Should Know About JavaScript
- HTML5: What You Have To Know For Interview
- What ASP.NET Developers Should Know About JavaScript
- Simple ASP.NET 2.0 Tips and Tricks that You May (or may not) have Heard About [from internet]
- What you need to know about transimpedance amplifiers – part 1
- Simple ASP.NET 2.0 Tips and Tricks that You May (or may not) have Heard About [from dwahlin]
- 转 译文:你需要知道的CSS3 动画技术 译自:What You Need To Know About Behavioral CSS
- you have to know about how to create user defined control.
- asp.net mysql无法查询中文;You have an error in your SQL syntax; check the manual that corresponds to
- What ASP.NET Developers Should Know About JavaScript
- What you need to know about AllowUnsafeUpdates (Part 1) [转载]
- You have to know how .Net code "managed"?
- What you need to know about AllowUnsafeUpdates (Part 2) [转载]
- 转: Simple ASP.NET 2.0 Tips and Tricks that You May (or may not) have Heard About (一些简单的、你可能已经知道或者不知道的ASP.NET 2.0技巧)
- The 4 server logs you NEED to know to fix any IIS / ASP.NET error
- [转]What you need to know about transimpedance amplifiers – part 1