I am new to ASP.NET MVC and am finding the options for displaying something like an SSN to be a bit much.
I have a need to display various types of unique ID’s, but the UI calls for the parts of them to be split out into different text boxes. For example, a Social Security Number with the first three digits in a text box, the next two digits in another text box and so on. I used jQuery auto-tab plugin to help the user quickly enter the data.
With regard to hooking this up to MVC3, it seems like I should use an Editor Template like this and use the [UIHint(“ssn”)] along with @Html.EditorFor:
@model string
@Html.TextBox("", Model != null ? Model.Substring(0, 2) : "")
@Html.TextBox("", Model != null ? Model.Substring(2, 5) : "")
@Html.TextBox("", Model != null ? Model.Substring(5, 3) : "")
Is this the best way to be going about this?
Using a custom editor template to split the SSN into 3 textboxes is definitely good but I am afraid it is only the first part (it is the way to present the view model inside the view).
But I guess that since you are presenting those values in textboxes you will expect the user to enter some values into these textboxes. And that you will probably want to fetch those values back into the SSN property of your view model when the user submits the form. This will be the second part. You might need a custom model binder to handle this. Hansleman blogged with an example.