I am trying to build a form which contains state as droplink and given datasource as states .
here's the snippets of my employee model and state.
i am getting all the states in the get action of form and passing them.
public ActionResult EmployeeForm()
{
var sitecoreService = new SitecoreService("master");
var statesFolder = sitecoreService.GetItem<States>("/sitecore/content/Global/States", x => x.LazyDisabled());
ViewBag.States = statesFolder.Children;
return View();
}
when i am trying to retreive the values then the state is mapped correctly but in the post action of form it is null.
here's the snippet of my form's state
<div class="form-group">
@Html.LabelFor(model => model.State, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.State, new SelectList(ViewBag.States,"Id","Name"), "select a state", new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.State, "", new { @class = "text-danger" })
</div>
</div>
Post action code:
public ActionResult CreateItem(EmployeeModel employeeModel)
{
ID id = new ID("{529829D4-9DE6-4E30-AF40-D67EA93D3271}");
var sitecoreService = new SitecoreService("master");
var parent = sitecoreService.GetItem<Employees>(id.Guid);
employeeModel.Name = "employee-" + employeeModel.EmployeeId;
sitecoreService.CreateItem(parent, employeeModel);
return View("~/Views/ContactBook/Success.cshtml", employeeModel);
}
state
inEmployeeModel
to string or ID and see if it's passed properly via the POST action? I don't think Glass will populate your objects in POST actions.