开发者

Adding data to database using MVC - with FK relationships

开发者 https://www.devze.com 2023-01-07 20:32 出处:网络
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 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 t开发者_Go百科o 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,


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


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.


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消