質問

I have a model...

public class PatientACOModel
{
    public int EncounterId { get; set; }
    public int PatientId { get; set; }
    public int EMPIID { get; set; }
    public int PopulationPatientID { get; set; }
    public string EditAddOrEditCurrent { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime? DateOfBirth { get; set; }
    //[UIHint("_PhoneNumFormatter")]
    public string Phone { get; set; }
 }

I want to specifically format my phone numbers. I just want to put a UIHint over my phone number and possibly other phone numbers. I don't want all strings to be formatted.

I am trying things like this...

@Model String
@if (Model != null) {
    String.Format("{0: (###) ###-####}", double.Parse(Model.ModelMetadata.Get));
}

That would be the display template referenced in my UIHint that is commented out. When I do that, none of my strings show up. What am I doing wrong?

In my display template how do I get the string to parse and then Format?

役に立ちましたか?

解決

First I would check if the _PhoneNumFormatter is defined in one of the following locations:

~/Views/Shared/DisplayTemplates/_PhoneNumFormatter.cshtml
~/Views/<Controller>/DisplayTemplates/_PhoneNumFormatter.cshtml

Next I think you wanted to format the string value directly from the Model property:

@model String

@if (!String.IsNullOrWhiteSpace(Model)) 
{
    @String.Format("{0: (###) ###-####}", Convert.ToInt64(Model))
}

This should work fine with the UIHint attribute, I've just checked in a sample application.

Code assumes that numbers in your region don't start with a leading 0 and that the Model property will contain strings containing only numeric characters, if not you should improve the Convert.ToInt64 part.

Hope it helps.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top