1

これは私を悩ませています、そして私は何が起こっているのか分かりません。数値を正しくフォーマットするために部分ビューを作成しました。

ファイル_PermitNumber.cshtmlがあり、次のもの/Views/Sharedが含まれています。

@model System.Int32
@Html.Raw(Model.ToString("00-0000"))

私のメインページで私はします:

@Html.DisplayFor(model => model.BuildingPermit.PermitId, "_PermitNumber")

model.BuildingPermit.PermitIdとして定義されますint

私の出力は、予想される12-0456ではなく120456です。

何が起こっている?

4

1 に答える 1

1

Why are you using a view for formatting your output? That seems like an unnecessary piece of complexity you are adding to your view. IMO a better location for this would be within a property of your view model:

public string FormattedPermitNumber
{
    get 
    {
         return PermitId.ToString("00-0000"));
    }
}

But to answer your question, if you were delete your partial view, you will see that your number is still displaying as 120456. In order to render your partial view you need to render the partial as follows:

@{ Html.RenderPartial("_PermitNumber",  Model.BuildingPermit.PermitId); }
于 2012-06-06T16:15:14.467 に答える