1

I am trying to build a form which contains state as droplink and given datasource as states enter image description here.

here's the snippets of my employee model and state.

enter image description here

enter image description here

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);                  
    }
5
  • And what is your post action code?
    – Marek Musielak
    Commented Feb 22, 2019 at 7:04
  • Added the post action to the question @MarekMusielak Commented Feb 22, 2019 at 8:44
  • So you're sending a state ID from your form in a POST action and expect that glass will autopopulate the employeeModel with a State object? Can you try to change the state in EmployeeModel 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.
    – Marek Musielak
    Commented Feb 22, 2019 at 9:50
  • Changing the state to guid done the trick. Thanks a lot @MarekMusielak Commented Feb 22, 2019 at 10:53
  • Comments converted into an answer
    – Marek Musielak
    Commented Feb 22, 2019 at 11:03

1 Answer 1

1

Looking at your code, you're sending a state ID from your form in a POST action back to the server. Your model contains State property, but it's an object of State class. It's not possible to populate object from ID in a POST action automatically OOTB.

What you should do instead is to change the type of State property in EmployeeModel class to Guid or string (and maybe call it StateId) and then retrieve the item from database:

public class EmployeeModel : DepartmentModel
{
    public Guid StateId { get; set }
    // ... other properties
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.