下拉列表,保存值的问题,返回空对象

时间:2018-10-11 19:50:08

标签: c# asp.net asp.net-mvc visual-studio model-view-controller

在我的项目中使用DDD,并且在使用下拉菜单时遇到问题

我已经可以在下拉列表中加载“ ClasseBarco”类的数据,但是我无法将选择保存在数据库中

“ BarcoViewModel”类与“ ClasseBarcoViewModel”类具有一对多关系。当我尝试在下拉菜单中保存所选选项时,出现以下错误:“ classeBarco为空”。使用编辑器对于我可以保存的东西,使用下拉菜单只是无法保存

调用其他3个类的ViewmodeClass

public class BarcoClasseViewModel


{
    public BarcoViewModel Barco { get; set; }

    public ClasseBarcoViewModel ClasseBarco { get; set; }

    public TipoOperacaoDoBarcoViewModel TipoOperacaoDoBarco{ get; set; }

   }

ViewModeBarco

   public class BarcoViewModel
   {
     public BarcoViewModel()
     {
        Id = Guid.NewGuid();
    }


    [Key]
    public Guid Id { get; set; }

    [Required(ErrorMessage = "Campo Obrigatório")]
    public string Nome { get; set; }

    [Required(ErrorMessage = "Campo Obrigatório")]
    public bool Ativo { get; set; }

    [Display(Name = "Registro SAP")]
    [Required(ErrorMessage = "Campo Obrigatório")]
    public int SapId { get; set; }

    [Display(Name = "Tancagem Água")]
    [Required(ErrorMessage = "Campo Obrigatório")]
    public int CapacidadeAgua { get; set; }

    [Display(Name = "Tancagem óleo")]
    [Required(ErrorMessage = "Campo Obrigatório")]
    public int CapacidadeOleo { get; set; }


    [Required(ErrorMessage = "Campo Obrigatório")]
    public int Velocidade { get; set; }

    [Required(ErrorMessage = "Preencha o campo E-mail")]
    [MaxLength(100, ErrorMessage = "Máximo {0} caracteres")]
    [EmailAddress(ErrorMessage = "Preencha um E-mail válido")]
    [Display(Name ="E-mail")]
    public string Email { get; set; }

    [Required(ErrorMessage = "Campo Obrigatório")]
    public string Setor { get; set; }

    [ScaffoldColumn(false)]
    public DateTime DataCadastro { get; set; }

    [ScaffoldColumn(false)]
    public bool Excluido { get; set; }

    [ScaffoldColumn(false)]
    public int ClasseBarcoId { get; set; }

    [ScaffoldColumn(false)]
    public ClasseBarcoViewModel classeBarco { get; set; }

    [ScaffoldColumn(false)]
    public TipoOperacaoDoBarcoViewModel TipoOperacaoDoBarco { get; set; }

    [ScaffoldColumn(false)]
    public DomainValidation.Validation.ValidationResult ValidationResult { get; set; }

}

}

ViewModelClasseBarco 公共类ClasseBarcoViewModel     {

    public ClasseBarcoViewModel()
    {
        Id = Guid.NewGuid();
    }

    [Key]
    public Guid Id { get; set; }

    [Display(Name ="Classe")]  
    [Required(ErrorMessage ="Campo Obrigatório")]
    public string Nome { get; set; }

    [ScaffoldColumn(false)]
    public bool Ativo { get; set; }


    [ScaffoldColumn(false)]
    public DateTime DataCadastro { get; set; }

    [ScaffoldColumn(false)]
    public Guid ClasseBarcoId { get; set; }

}

控制器

      [Authorize(Roles = "Administrator")]
      [Route("criar-novo")]
       public ActionResult Create()
      {
        ViewBag.ClasseBarcoId = new   SelectList(_barcoAppService.ObterTodasAsClasses(), "ClasseBarcoId", "Nome");

        return PartialView();
    }

    // POST: Barcos/Create
    [Authorize(Roles = "Administrator")]
    [Route("criar-novo")]
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(BarcoClasseViewModel barcoClasseViewModel)
    {
        if (!ModelState.IsValid) return PartialView(barcoClasseViewModel);

        ViewBag.ClasseBarcoId = new SelectList(_barcoAppService.ObterTodasAsClasses(), "ClasseBarcoId", "Nome", barcoClasseViewModel.Barco.ClasseBarcoId);

        var barcoEnd = _barcoAppService.Add(barcoClasseViewModel);


        if (barcoEnd.Barco.ValidationResult.IsValid) return RedirectToAction("Index");

        PopularModelStateComErros(barcoEnd.Barco.ValidationResult);


        return PartialView(barcoClasseViewModel);


    }

视图

 <div class="form-group">
              @Html.LabelFor(model => model.Barco.ClasseBarcoId,   htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @Html.DropDownList("ClasseBarcoId", String.Empty)
                @Html.ValidationMessageFor(model => model.Barco.ClasseBarcoId, "", new { @class = "text-danger" })
            </div>
        </div>

1 个答案:

答案 0 :(得分:1)

您必须在构造函数中委派新的类,因为如果未设置属性,则在获取值时会出现异常。

public class BarcoClasseViewModel

public BarcoClasseViewModel()
{
  Barco = new BarcoViewModel();
  ClasseBarco = new ClasseBarcoViewModel();
  TipoOperacaoDoBarco = new TipoOperacaoDoBarcoViewModel();
}

{
  public BarcoViewModel Barco { get; set; }
  public ClasseBarcoViewModel ClasseBarco { get; set; }
  public TipoOperacaoDoBarcoViewModel TipoOperacaoDoBarco{ get; set; }
}