Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

mvc, razor with editor view

Posted on 2013-11-06
4
Medium Priority
?
1,063 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…
Suggested Courses

721 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