Download presentation
Presentation is loading. Please wait.
Published byCecil Merritt Modified over 9 years ago
1
BIT 286: Web Applications Lecture 04 : Thursday, January 15, 2015 ASP.Net MVC -
2
Examining the Edit Methods and Edit View http://www.asp.net/mvc/overview/getting- started/introduction/examining-the-edit-methods-and- edit-view http://www.asp.net/mvc/overview/getting- started/introduction/examining-the-edit-methods-and- edit-view Using Entity Framework attributes to annotate model fields Using the Html helper (ActionLink method to generate path via routing) Postbacks 2 nd method with bound object Redirecting Anti-forgery token 2
3
Using EF attributes to better define DB table using System; using System.ComponentModel.DataAnnotations; using System.Data.Entity; namespace MvcMovie.Models { public class Movie { public int ID { get; set; } public string Title { get; set; } [Display(Name = "Release Date")] // default was “ReleaseDate” [DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] public DateTime ReleaseDate { get; set; } public string Genre { get; set; } public decimal Price { get; set; } } public class MovieDBContext : DbContext { public DbSet Movies { get; set; } } 3
4
C# Attributes (aka “Annotations” in Java) “C# provides a mechanism for defining declarative tags, called attributes, which you can place on certain entities in your source code to specify additional information.” “Entities” – classes, methods, instance variables, etc. “The information that attributes contain can be retrieved at run time through reflection. You can use predefined attributes or you can define your own custom attributes. ” -- MSDNreflectionpredefined attributes MSDN NUnit used these ( [Test], [TestFixture], [Category], etc) 4
5
Date formats There’s a page for the DisplayFormat attributeDisplayFormat According to the docs, this can usedocs Various standard formatting codes for C#’s date&time typesstandard formatting codes for C#’s date&time types Custom formats (This is what the page is using) Custom formats For example, "{0:dddd, MMMM d, yyyy}" will produce: Friday, January 1, 1999 5
6
The Html helper class How to link from, say, the Index page to the Details page? In Views/Movies/Index.cshtml @Html.ActionLink("Edit", "Edit", new { id=item.ID }) | @Html.ActionLink("Details", "Details", new { id=item.ID }) | @Html.ActionLink("Delete", "Delete", new { id=item.ID }) 6
7
Html.ActionLink method @Html.ActionLink("Details", "Details", new { id=item.ID }) | First arg is the text to put on the page Second arg is the name of the method on this controller to link to Movies.Details Third arg is an anonymous object with info needed to build the path This actually builds the path based on the routing info in App_Data/RouteConfig.cs You can change your routing and all the links will change Details at MSDN Details at MSDN There are other versions if you want to link to a different controller (e.g., back to the overall home page) 7
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.