tags:

views:

88

answers:

3

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?

A: 

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...

Greg Roberts
Greg, I appreciate your response, and the post from Stephen is helping but I'm still confused.To clarify, I have a Customers table and one of the fields is CountyID, which is set up as a foregin key to a Counties table. When I create or edit a customer, I have set up dropdown select lists, which are correctly displaying the CountyName field instead of the CountyID. Unfortunately when I save the new customer, the detail view shows "7" instead of "Pinellas". I'm trying to code the details view to display a county name instead of an ID#
Ben
Ben, you are almost there you just need to query for the county name by id in your controller detail method and add it to your model or viewdata. Once it is in your model you can display it on the view instead of the id. MVC won't do this for you out of the box when it creates the templates, you need to implement this.
Greg Roberts
I suppose my confusion is that I have my dropdowns working correctly so they display a name instead of a # when I'm either creating or editing, and I understand how the SelectList is working in those controller methods, so what would I use in the details view instead of a selectlist? If I already have a table for counties could you show me an example of what the query would look like so that it gets the customer's county ID and translates it to a county name?
Ben
Ok, it's hard for me to say what your query will look like without knowing how you are doing your data access. The raw Sql statement would be something like, "select CountyName From Counties Where CountyId = x". If you are using EntityFramework, you could include the county row in your result for that entity.
Greg Roberts
In terms of the Html. If this page isn't an edit page, then just write it out in a div or p tag like <p><%= Html.Encode(Model.CountyName) %></p>
Greg Roberts
+1  A: 

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.

Swanny
Swanny,That is basicaly what I have in my Edit and Create views because the user needs to select a county - however, I'm lost on what to put in my Detail view to just display the county name that was picked. currently my view just shows "7" for county instead of "pinellas"
Ben
Yeah, my bad. See above. You might want to just use "CountyID" as your ViewData key, that would make life easier.
Swanny
If it's your Detail view, why not just show the county name as text or a read only text box. You only want this to be changed in your edit yiew?
Swanny
Correct - I don't want it to be editable in the details view. I just want it to display the county name instead of the county ID.
Ben
So in your controller, given you have the id of the county that this entity has, say countyId:ViewData["CountyName"] = db.Counties.First( c => c.CountyID == countyId).Select( c => c.CountyName )and then just render that value in your view:<%= Html.Encode( ViewData["CountyName"] ) %>Is that what your after?
Swanny
Swanny - Absolutely helps! - Although I'm still trying to figure out how to locate the countyID. When I stick that code in my method, I get the error that countyID is not defined in the current context. I assume I need a line of code that retrieves the Customer by their ID, and then sets countyID to be the ID that's saved for that customer. Almost there... !
Ben
Ok. So in your Create/Edit view, what do you do with the selected CountyId? I assume you would be storing this as the attribute of some entity, say the CountyId column of an Employee or Town or something (that is the foreign key). So in the Detail view you should be retrieving this information, similar to what your Edit view should be doing. From this information you should be able to get the "countyId" value. So something like:var countyId = Employee.CountyId;or perhaps:var countyId = dbEmployee.EmployeeCountyId
Swanny
Incidentally, in your Edit view, you just need to include this value as the 4th parameter to the SelectList() constructor to specify which of the drop down list values you want preselected.
Swanny
A: 

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 :)

Ben