Solved

mvc, razor with editor view

Posted on 2013-11-06
4
874 Views
Last Modified: 2013-11-22
http://forums.asp.net/post/4647234.aspx

I got above url reference to create date picker and it works very well. I would like to extend the same style with phone number masking(xxx)xxx-xxxx, and  ssn masking (xxx-xx-xxxx),

and hope some experts can share with me how to do it.

Thanks for your helps
0
Comment
Question by:solution1368
  • 3
4 Comments
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 39630093
Hi,

Take for instance the following model:

    public class User
    {
        public DateTime DateOfBirth { get; set; }
        [UIHint("Phone")]
        public int Phone { get; set; }
        [UIHint("SSN")]
        public int SSN { get; set; }
        //public String Phone { get; set; }
    }

Open in new window


Notice the 'UIHint'. This will tell the engine which template to apply.

And the following Index view:

@using MvcApplication2.Models
@model User
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>
    Index</h2>

<div class="editor-field">
    @Html.DisplayFor(model => model.DateOfBirth)
</div>
<div class="editor-field">
    @Html.DisplayFor(model => model.Phone)
</div>
<div class="editor-field">
    @Html.DisplayFor(model => model.SSN)
</div>

Open in new window


In the Home controller you have the following Action:
        public ActionResult Index()
        {
            Models.User user = new Models.User() 
            { 
                DateOfBirth = DateTime.Parse("16/09/2013"), 
                Phone = 1234567890, 
                SSN = 321654987 
            };
            return View(user);
        }

Open in new window


In your Shared folder add a new folder DisplayTemplates. You may even want to add one for editors, called EditorTemplates.

In the DisplayTemplates folder, add a new view called Phone. This will be the template for your Phone fields. Make sure you make this a partial (check the partial view checkbox).

In this file add the following code:
@model System.Int32
@Html.Label("", (Model != null ? Model.ToString("(###)###-####") : string.Empty))

Open in new window


Similarly add another view in the same folder, called SSN and paste the following code:
@model System.Int32
@Html.Label("", (Model != null ? Model.ToString("(###-##-####)") : string.Empty))

Open in new window


Now you should be ready to go.

Again, keep in mind that, since you don't want the same format for all integers in your views, you have to decorate your properties with the according UIHint.

Good luck,
Giannis
0
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 39630095
By the way, thanks you very much for the article. I am new in MVC too and hadn't noticed that i can actually do this. Now i am much better.... :-)
0
 

Author Comment

by:solution1368
ID: 39648842
if I just added to EditorTemplates. Will that work?
0
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 total points
ID: 39652935
I think you should just allow the user to add a number in these fields when in edit mode. The in display mode you format them as you like.

If you do want to mask the edit (there is no point since you are going to store it as a number) the you can use some js library for this. This google search may help:
https://www.google.gr/search?q=js+masked+input

Giannis
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question