Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Professional ASP.NET MVC 3
|
BOOK: Professional ASP.NET MVC 3
This is the forum to discuss the Wrox book Professional ASP.NET MVC 3 by Jon Galloway, Phil Haack, Brad Wilson, K. Scott Allen; ISBN: 978-1-1180-7658-3
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET MVC 3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 9th, 2012, 09:42 AM
Authorized User
 
Join Date: Jul 2011
Posts: 24
Thanks: 3
Thanked 0 Times in 0 Posts
Default Need to use DropDownListfor to lookup vaules from another table

Hi,

I am trying to achieve something which should be a very common practice. I have 2 tables: Department and Category (1 department to many categories).

When I input a new Category, I want to have a dropdownlist to select a department, then enter the category, and save.

I have managed to get it working but I have to repeat:
Quote:
//populate dropdown list with departments
DepartmentRepository departmentRepository = new DepartmentRepository();
var departments = departmentRepository.FindAllDepartment();

IEnumerable<SelectListItem> items = departments
.Select(d => new SelectListItem
{
Value = d.Dept_ID.ToString(),
Text = d.Department1
});


ViewBag.Dept_ID = items;
many times - for create (get and post) and edit (get and post). I am sure there is a short winded way to do this? Also I don't think using ViewBag is the best option for this??

Could anyone kindly help me, and perhaps give me a good example to work from? I have already spent a few days googling but there is not a straight forward example.

Below are the codes for my Edit (get and post) in the Category Controller:
Quote:
public ActionResult Edit(int id)
{
Category category = categoryRepository.GetCategory(id);

//populate dropdown list with departments
DepartmentRepository departmentRepository = new DepartmentRepository();
var departments = departmentRepository.FindAllDepartment();

IEnumerable<SelectListItem> items = departments
.Select(d => new SelectListItem
{
Value = d.Dept_ID.ToString(),
Text = d.Department1
});


ViewBag.Dept_ID = items;

ViewBag.Mode = "Edit";


return View(new CategoryFormViewModel(category));
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, string button, FormCollection collection)
{
if (button == "cancel")
return RedirectToAction("Index", "Category");

//populate dropdown list with departments
DepartmentRepository departmentRepository = new DepartmentRepository();
var departments = departmentRepository.FindAllDepartment();

IEnumerable<SelectListItem> items = departments
.Select(d => new SelectListItem
{
Value = d.Dept_ID.ToString(),
Text = d.Department1
});
ViewBag.Dept_ID = items;

ViewBag.Mode = "Edit";

Category category = categoryRepository.GetCategory(id);

if (collection["Category.Dept_ID"] != "")
{
category.Dept_ID = Convert.ToInt32(collection["Category.Dept_ID"]);
}
else
{
category.Dept_ID = null;
}

category.Category1 = collection["Category1"];

try
{
UpdateModel(category);
categoryRepository.Save();
return RedirectToAction("Index", new { id = category.Category_ID });
}

catch
{
ModelState.AddModelErrors(category.GetRuleViolatio ns());
return View(new CategoryFormViewModel(category));
}
}
My CategoryFormViewModel is as below:
Quote:
//
// ViewModel Classes

public class CategoryFormViewModel
{
//properties
public Category Category { get; private set; }

//Constructor
public CategoryFormViewModel(Category category)
{
Category = category;
}

}
And in my View I have:
Quote:
<p>
<label for="Dept_ID">Department:</label>
<%

if (ViewBag.Mode == "Create")
{
%>
<%=Html.DropDownList("Dept_ID",
(IEnumerable<SelectListItem>)ViewBag.Dept_ID,"--Select One--")%>
<%
}
else
{
%>
<%=Html.DropDownListFor(m => m.Category.Dept_ID,
(IEnumerable<SelectListItem>)ViewBag.Dept_ID, "--Select One--")%>
<%}
%>
<%=Html.ValidationMessage("Dept_ID", "*")%>
</p>
Please be patient with me as I am a fluent Classic ASP developer but NOT an ASP.NET MVC one!

Many thanks in advance.





Similar Threads
Thread Thread Starter Forum Replies Last Post
lookup value in table Vince_421 Access 16 February 13th, 2007 08:15 AM
Lookup (Dispatch) Table CNewbie C++ Programming 5 November 2nd, 2004 04:12 PM
Lookup Table Problem mikericc Access VBA 2 January 19th, 2004 07:11 PM
lookup table current field. Squid Access 0 December 21st, 2003 07:08 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.