views:

43

answers:

3

Hi- I have a database and I am using the Entity Framework with Linq 2 Enities. I am working with ASP.NET MVC 2.

I have a database with two tables as shown bellow:

Classifieds             Catergories
ID(PK)                  CatID(PK)
CatID(FK)               CatName
Desc
...

Obviously when the user adds a new classified and when they come to pick the category they don't want to see CatID's so my question is this. How do I let the user add a classified by selecting the CatName?

I am using strongly typed views. Restructuring the database is out of the question. Answer in C# please.

Thanks,

A: 

You could use a drop-down exposing the category name, but using the Id as the value:

<%-- Using your Model has a list of Category objects set --%>
<%= Html.DropDownList("classified.Category.Id", new SelectList(Model.Categories, "Id", "Name") %> 

Your controller action might look something like:

public ActionResult Save(Classified classified)
{
   int categoryId = classified.Category.Id;
}
JonoW
Yeah I have done something simmilar in MS Access so I thought that this was the way to go. I just didn't know how to implement it. Many thanks I will give it a go.
Csharper
I can't do "Model.Catergories" in the selectList because my model is empty because I am adding data, I tried it and I am getting a null reference exception. Any idea on how to pass the catergories to the view I have tried ViewData.
Csharper
Yeah it depends how you structure your Model. You should be able to use the ViewData dictionary, so in your controller:ViewData["Categories"] = categories; //enurable list of category objsand then in your view:<%= Html.DropDownList("classified.Category.Id", new SelectList((IEnumerable)ViewData["Categories"], "Id", "Name") %>
JonoW
A: 

Get the existing categories from the database (preferrably via a repository, but for brevity I just show the Linq to Entities query):

var db = new YourObjectContextImplementation();
var categories = db.Categories.AsEnumerable();

Forward these to your view via a view model. In your view, you output a form somehow to enable the user to add a classified. Instead of giving them a textbox (or select list) for the category Id, you give them a select list where the value is the id, but the text/inner html is the category name. That way, the user only sees the name, but your action gets an integer parameter passed to it.


UPDATE in response to comment:

I assume you are currently passing a new Classified object as view model, and then using the EditorFor helpers? All good, but you should probably wrap the Classified object in a specific view model, for example a ClassifiedAddModel as follows:

public class ClassifiedAddModel
{
    public Classified NewClassified { get; set; }
    public IEnumerable<Category> ExistingCategories { get; set; }
}

Then you can instantiate and populate the ClassifiedAddModel object in the controller action, pass it along as the model to your view, and build the form using syntax like

<%: Html.EditorFor(Model.NewClassified.Description) %>
<%: Html.EditorFor(Model.Categories "CategoriesEditor") %>

where "CategoriesEditor" is the name of a custom view model that takes categories and renders a select list with them. The main point with the above example, however, is to show how you can access both properties on the Classified object, and all the existing categories.

Tomas Lycken
I can't do "Model.Catergories" in the selectList because my model is empty because I am adding data, I tried it and I am getting a null reference exception. Any idea on how to pass the catergories to the view I have tried ViewData.
Csharper
@simon, see my update.
Tomas Lycken
A: 

I have come up with this solution:

Controller:

    var Catergories = classifiedsRepositry.GetClassifiedsCategories().AsEnumerable();

    ViewData["CatID"] = new SelectList(Catergories, "CatID" , "Category");

View:

<%: Html.DropDownListFor(model => model.CatID, (SelectList)ViewData["CatID"],"--Pick A Category--")%>

And that is it dead simple and it works fine!

Csharper