Html.DropDownList startup problem

I get this error:

The ViewData element with the key "DepartmentId" is of type "System.Int32", but must be of type "IEnumerable".

with the following setting. I am not sure how to solve it. An error occurs in the model code. This line: public void MapTo (Person domainModel). I am using AutoMapper to display the ViewModel back in the DomainModel (changing the initial display of the DomainModel to the ViewModel).

Domain model (using LINQ to SQL, so this is a partial class):

public partial class Person { }

// Validation rules
public class Person_Validation
{
    [HiddenInput(DisplayValue = false)]
    [ScaffoldColumn(false)]
    public object PersonId { get; set; }

    [HiddenInput(DisplayValue = false)]
    [ScaffoldColumn(false)]
    public object DepartmentId { get; set; }

    [DisplayName("Employee Name")]
    [Required(ErrorMessage = "Employee Name is required")]
    [StringLength(50, ErrorMessage = "Employee Name cannot be more than 50 characters")]
    public object Name { get; set; }

    [HiddenInput(DisplayValue = false)]        
    public object Active { get; set; }

    [HiddenInput(DisplayValue = false)]
    public object DateAdded { get; set; }

    [HiddenInput(DisplayValue = false)]
    public object DateDeleted { get; set; }

    public object Department { get; set; }     
}

This is my kind of model:

public class PersonViewModel
{       
    public object PersonId { get; set; }

    public object DepartmentId { get; set; }

    public object Name { get; set; }

    public object Active { get; set; }

    public object DateAdded { get; set; }

    public object DateDeleted { get; set; }    

    public object DepartmentName { get; set; }

    //helper method
    public void MapTo(Person domainModel)
    {
        Mapper.Map(this, domainModel);
    }
}

Controller class code:

[HttpPost]        
public ActionResult Edit(PersonViewModel viewModel)
{
    var domainModel = new Person();                                       
    try                    
    {                      
        viewModel.MapTo(domainModel);
        UpdateModel(domainModel);
        _personRepository.Save();
        return RedirectToAction("Index", "Person");
    }                      
    catch                  
    {                      
        return View(viewModel);
    }                      
} 

And my view HTML code:

<div class="editor-field">                
<%: Html.DropDownList("DepartmentId", (IEnumerable<SelectListItem>)ViewData["DepartmentList"])%>                                     
<%: Html.ValidationMessageFor(model => model.DepartmentId) %>
</div>
+3
source share
1 answer

You are not following the best mvc application development practices.

: Html.DropDownList IEnumerable<SelectListItem> , int (DepartmentId)

ViewModel MapTo,

, :

if(!ModelState.IsValid)
{
   //rebuild the viewmodel and return the view
}

( ) Global.asax Application_Error

HiddenInput, ScaffoldColumn, Validation , UI, ViewModel


↔ viewmodel. Samples

, - ValueInjecter ( )

0

Source: https://habr.com/ru/post/1761479/


All Articles