Solved

mvc, razor with editor view

Posted on 2013-11-06
4
914 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 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why use this lambda? 12 61
ASP.net Markup for Sub Menu 2 40
Duplicate Elements with JS / jQuery 3 24
What is the correct way to check for empty value in input field 2 16
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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)

756 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