Ben
Ben

Reputation: 1023

Converting an ID into a name in ASP.Net MVC

I'm trying to convert an ID into a name when I render a "detail" view in my application. I have successfully been able to display the name in my "edit" and "create" views by using the code:

In my controller:

ViewData["countyViewData"] = new SelectList(db.Counties, "CountyID", "CountyName");

In my views:

<%= Html.DropDownList("CountyID", ViewData["countyViewData"] as SelectList)%>

The heart of my question is what do I need in my controller and in my details view to display as the CountyName instead of the ID? I have this in the view currently:

<%= Html.Encode(Model.CountyID) %>

I assume I need some ViewData code in the controller to tell the view what county name to use for each ID.

Thanks in advance for helping a new programmer!

Added to Clarify: My Details view for a customer displays the value which is the CountyID, and I want it to display the CountyName which is stored in another table. What needs to be in the controller and the view so that the CountyName displays?

Upvotes: 0

Views: 2766

Answers (3)

Ben
Ben

Reputation: 1023

I found it - and thanks to all who helped me get to the finish line! Here's the code that ended up working for me:

In my Model:

public County GetCounty(int id)
    {
        return db.Counties.SingleOrDefault(d => d.CountyID == id);
    }

In my Comtroller:

int countyID = customer.CountyID;
County county = customerRepository.GetCounty(countyID);
ViewData["CountyName"] = county.CountyName;

And in my View:

<%= Html.Encode(ViewData["CountyName"]) %>

I probably would have gotten a quicker solution, but I know I'm not the best at asking the question properly :)

Upvotes: 0

Swanny
Swanny

Reputation: 2418

Create an IEnumerable or List of SelectListItem with the Text property what you want to show (county name), the Value property for what value you want to return (county id) and Selected true if that item is to be preselected.

So possibly:

ViewData["counties"] = new SelectList(db.Counties, "CountyID", "CountyName").Select( c => new SelectListItem { Text = c.CountyName, Value = c.CountyId, Selected = false } );

Now all you have to do in your view is:

<%= Html.DropDownList( "Counties") %>

It should be that simple or even simpler if I forgot something. I'm pretty sure the Selected property is false by default so you could drop that if you want.

Good luck.


Arghh. I forgot what SelectList was. I thought it was one of yours.

So forget all of the above and all you need to do is in your view have:

<%= Html.DropDownList( "countyViewData") %>

Swanny.

Upvotes: 1

Greg Roberts
Greg Roberts

Reputation: 2562

I'm a little confused by your question, but I let me try to summarize what you are saying...

Give an CountyID for table Counties, how do you show the name in the detail view of an entity?

I'm assuming your entity has a CountyID that is a foreign key to the Counties table. In your controller, or wherever you are loading your entity you need to bring along the joined tables data (left/inner join) or you can do a separate query for the county info for that ID (get county by id).

As a general rule of practice, I know a lot of the samples use the ViewData[""] syntax, you should probably always define a strongly typed view/model. This will make your life easier and make the code easier to deal with.

For example in your details view, you can define the county name and ID and display them as you wish in your view. For a decent example check out A post from Stephen Walther here

public class BensViewModel{
  public string CountyName {get;set;}
  public int  CountyId {get;set;}
}

If this doesn't help, please try to clarify your question a little more for us...

Upvotes: 0

Related Questions