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.