Wednesday 7 November 2012

MVC Tips


I have been working with MVC projects for a while and one common issue I have is find out how to do something easy. I will be updating this article as I go along and add the issues I have had and my solution to this. This is by no means the best way, but it is used as it works.

DataAnnotations

 

Formating DateTime using Html.Editor

 Displaying formated DateTime
 
I needed to display custom format on date in editor for.
You can use decoration to achieve custom formating of datetime field in your model. 
 
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime Date { get; set; }

Bare in mind that you need to use
@Html.Editor(model=>model.Date) for this field.

Routing

Custom route with aspx page


I have one aspx page which is used in webforms and my project in mvc. I want to have nice routing in place and do not want to display to the user name of the page.

original page is somethinng like: http://localhost:9999/oldwebsite/SayHelloWorld.aspx
my desired name of the page : http://localhost:9999/SayHelloWorld

To achieve this we can use global.asax with custom route in register routes, into which you need to add your custom route.

routes.MapPageRoute("SayHelloWorld", "SayHelloWorld", "/oldwebsite/SayHelloWorld.aspx");

No comments:

Post a Comment