3
I’m having a question on how popular and then pick up the selected item from a DropDownListFor
, I am using DDD architecture and Entity Framework.
In case here my class ServiceProviderViewModel
has to have relationship with other classes. I would like to know how to do to popular this Helper in View of Create
.
Follows codes:
public class ServiceProviderViewModel
{
[Key]
public int ServiceProviderId { get; set; }
[Required(ErrorMessage = "Por favor, informe o nome do colaborador.")]
[Display(Name = "Nome:")]
public string Name { get; set; }
[Display(Name = "Nome da Mãe:")]
public string MotherName { get; set; }
[Display(Name = "Nome do Pai:")]
public string FatherName { get; set; }
[Display(Name = "E-mail:")]
[EmailAddress(ErrorMessage = "Por favor, informe um formato de e-mail válido.")]
public string Email { get; set; }
[Display(Name = "Nascimento:")]
public DateTime Birth { get; set; }
[ScaffoldColumn(false)]
public DateTime DateRegister { get; set; }
[ScaffoldColumn(false)]
public DateTime DateModified { get; set; }
[Required(ErrorMessage = "Por favor, informe o departamento para o colocaborador.")]
[Display(Name = "Departamento:")]
public int DepartamentId { get; set; }
[Required(ErrorMessage = "Por favor, informe o departamento para o colocaborador.")]
[Display(Name = "Cargo/Função:")]
public int PositionId { get; set; }
public virtual DepartamentViewModel Departaments { get; set; }
public virtual PositionViewModel Positions { get; set; }
public virtual IEnumerable<ServiceProviderAddressViewModel> ServiceProviderAddress { get; set; }
public virtual IEnumerable<ServiceProviderPhoneViewModel> ServiceProviderPhone { get; set; }
public virtual IEnumerable<ServiceProviderInfoViewModel> ServiceProviderInfo { get; set; }
public virtual IEnumerable<InfoBankViewModel> InfoBank { get; set; }
}
In the Controller:
public class ServiceProviderController : Controller
{
private readonly IServiceProviderAppService _serviceProviderApp;
public ServiceProviderController(IServiceProviderAppService serviceProviderApp)
{
_serviceProviderApp = serviceProviderApp;
}
// GET: ServiceProvider
public ActionResult Index()
{
var serviceProviderViewModel = Mapper.Map<IEnumerable<ServiceProvider>, IEnumerable<ServiceProviderViewModel>>(_serviceProviderApp.GetAll());
return View(serviceProviderViewModel);
}
// GET: ServiceProvider/Create
public ActionResult Create()
{
return View();
}
// POST: ServiceProvider/Create
[HttpPost]
public ActionResult Create(ServiceProviderViewModel serviceProvider)
{
if (ModelState.IsValid)
{
var serviceProviderDomain = Mapper.Map<ServiceProviderViewModel, ServiceProvider>(serviceProvider);
_serviceProviderApp.Add(serviceProviderDomain);
_serviceProviderApp.Save();
return RedirectToAction("Index");
}
return View(serviceProvider);
}
...
For each class that ServiceProvider
relates have the models specific to the realization of CRUD. I just don’t know how I should do to run this mixture of models among the views and controllers.
But since the Position class is another model, should I make a call from it inside the Serviceprovider controller? The idea isn’t exactly to create separate controllers for each model?
– Uitan Maciel
@Uitanmaciel, I spoke Position only as illustration. Instead of Position put the model referring to your combo.
– Vinicius Grund
It’s working fine. It’s populating the Dropdownlistfor. However you are giving error: An Exception of type 'System.Invalidoperationexception' occurred in System.Web.Mvc.dll but was not handled in user code Additional information: The Viewdata item that has the key 'Departamentid' is of type 'System.Int32' but must be of type 'Ienumerable<Selectlistitem>'. You’re having trouble converting the guy.
– Uitan Maciel
Maybe I need to see your code, but understand that the Obtain positions method (or whatever the name of your method needs to return something like Ienumerable<Position>, with the Position class having a Positionid property as key and a Description property that must be equal to the last two parameters passed to the New Selectlist.
– Vinicius Grund
look at the code below:
– Uitan Maciel
@Uitanmaciel, I looked at the code you posted and it seems ok to me. Complicated to know exactly what the problem is now. Probably you missed some detail, try debugging, see which line the problem occurs, see innerexception. Check the implementation of the Departament and repository class. It seems that the problem is to do the Get and not the Post, since in the post you do not use Viewbag.
– Vinicius Grund