views:

18

answers:

0

HI All,

I have created a dropdown for my view which works well up to saving the data. The Model State shows invalid on the object that is selected in my dropdown list.

EG. I have a "Asset" which has a "Project" dropdown list. When I save the Asset the correct ID is in place for the Project from the dropdown list, but the Project itself is not known and its properties are all null, Even though the ID is correct and can be assigned to the Asset.

Basically the save would work if the model state was valid.

I am not sure what I have to do here.

Relevant code:

Setup:

ViewData["AspectTypes"] = new SelectList(aspectRepository.GetAll(), "EntityGUID", "Name");
        ViewData["Projects"] = new SelectList(projectRepository.GetAll(), "EntityGUID", "Name");

View:

         <tr>
            <td class="fieldLabel"><%= Html.LabelFor(model => model.Project)%></td>
            <td class="fieldControl"><%= Html.DropDownListFor(model => model.Project.EntityGUID, (IEnumerable<SelectListItem>)ViewData["Projects"], new { name = "Project" })%></td>
            <td class="fieldControl"><%= Html.Encode(Model.Project.Name)%></td>
            <td class="error"><%= Html.ValidationMessageFor(model => model.Project)%></td>
        </tr>

Save:

if (ModelState.IsValid)
{
 lotRepository.Update(entity);
 return View("Search", lotRepository.GetAll());
}

Any assistance would be greatly appreciated.