Controller
public ActionResult Create()
{
return View(new PageViewModel());
}
[HttpPost]
public ActionResult Create(Page page)
{
try
{
repPage.Add(page);
repPage.Save();
return RedirectToAction("Edit");
}
catch
{
return View(new PageViewModel());
}
}
PageViewModel
public class PageViewModel
{
public Page Page { get; set; }
public List<Template> Templates { get; set; }
private TemplateRepository repTemplates = new TemplateRepository();
public PageViewModel()
{
Page = new Page();
Templates = repTemplates.GetAllTemplates().ToList();
}
}
Parts of my View
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Website.Models.PageViewModel>" %>
<%= Html.TextBoxFor(model => model.Page.Name, new { @style = "width:300px;" })%>
<%= Html.DropDownListFor(model => model.Page.Template, new SelectList(Model.Templates, "ID", "Name"), new { @style = "width:306px;" })%>
Template:
ID
Name
Page:
ID
Name
TemplateID
My dropdownlist is populated correctly in the view, no problems there. My problem is that I dont get the selected value from the dropdownlist.
In my controller I i put a breakpoint in Edit
and see that the Name
textbox is populated with the value I type into it. But the selected from the dropdownlist is set to null.
Am I missing something, I thought it should set the correct Template
value into the Page
object. What am I doing wrong?