سؤال

I am using to show controller and action name as below to use in my Views-BreadCrumb

@ViewContext.RouteData.Values["Controller"] / @ViewContext.RouteData.Values["Action"]

Output of this query is

HrMapPersonCertificate / Create

But i want to show different name for controllers. IE : Certification / New Certificate

Can i change the display names of actions and controllers?

EDITED

Actually i have some trouble with your solution. I did what you said but ie. in my Person model, now it has two more variable, route1 and route2. Route1 = "Person Information", Route2 = "Action Name : View Person". But i am using the same view also while editind or inserting with using this model. I am in trouble.

هل كانت مفيدة؟

المحلول

You should not be using the internal names of Controllers and Actions for displaying routes on the web site. This will make things hard to maintain and customize on the view-side of things (as you can see with this question) and inappropriately mixes the meta-data of your server side with the presentation level.

A better approach would be to create a BaseModel object that always contains the values that you want to display in your route:

public class BaseModel 
{
  public string Route1 {get; set;}
  public string Route2 {get; set;}
}

The setup can obviously change - you could use different names or even a List<string> of names to store more than two, but the idea is to not be hard-coding the controller and action names into your view.

Then use pass this model through to every view and use these values to display your route.

@model BaseModel
...
@Model.Route1 / @Model.Route2

Every single model that you pass to every single View on your site that uses this layout should inherit from BaseModel. Thus these properties will be available to use on every single view. So if your current model on a given page is called MyModel, just change its definition to be public class MyModel : BaseModel and you can then use the BaseModel properties as well.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top