Mike Wills
Mike Wills

Reputation: 21265

Why doesn't my partial view work?

This is bugging me and I don't know what is going on. I created a partial view to format a number properly.

The file _PermitNumber.cshtml is in /Views/Shared and contains:

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

On my main page I do:

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

model.BuildingPermit.PermitId is defined as an int.

My output is 120456 instead of the expected 12-0456.

What is going on?

Upvotes: 1

Views: 217

Answers (1)

Jesse
Jesse

Reputation: 8393

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); }

Upvotes: 1

Related Questions