views:

397

answers:

1

I usually use the Ruby on Rails framework but for this project I need to work with ASP.NET MVC and I'm confused.

Basically I have 1 table :

*Page = (id, name, category_id)*

Category_id is actually a link to a page (a page can be linked to another page). I set up foreign keys and everything, the model is up and working.

My question is : How to I link one page to another? If I try to set the category_id, it doesn't seem to be working... Is there a simple way of creating a dropdown menu from the Page model and use it in the Create/Edit actions of my application?

+1  A: 

You need a controller with an action like

public ActionResult DetailPage(int categoryId)
{
   return View(new ModelObject(categoryId));
}

Then have a form on the page made using the Html Helper Form, with your dropdown in it. The drodown should be made using the HTml helper dropdown, and should have categoryId as it's id. Like this;

<%=Html.DropDownList("categoryId",ViewData["CategoryID"])%>

With this in the controller or model for your page with the dropdown;

NorthwindDataContext db = new NorthwindDataContext();
var categories = from c in db.Categories select c;
ViewData["CategoryID"] = new SelectList(
   categories, "CategoryID", "CategoryName");

Hope this helps

Mark Dickinson
It didn't solved it right away but lead me directly to the solution that's working for me, which is:var categories = from c in _entities.PageSet select c;ViewData["category_id"] = new SelectList(categories, "id", "title");<%= Html.DropDownList("category_id") %>
marcgg