Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Professional ASP.NET MVC 3
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 9th, 2012, 09:42 AM
Authorized User
Points: 95, Level: 1
Points: 95, Level: 1 Points: 95, Level: 1 Points: 95, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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



All times are GMT -4. The time now is 02:50 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.