值不能为空。参数名称:MVC 5 DropdownList项目

时间:2018-10-15 08:41:35

标签: asp.net asp.net-mvc asp.net-mvc-4 asp.net-mvc-5

我正在尝试创建一个下拉列表,但是出现了标题显示的错误。我尝试了不同的方法,例如将IEnumerable用于我的ViewModel以及错误数量越来越大。我只学习了一个月左右的ASP.NET MVC,我对这些错误感到困惑。非常感谢那些知道如何解决此错误并有时间解释原因的人,谢谢您,先生!

这是我的PetRescuedViewModel

public class PetRescuedViewModel
{
    public PetRescued PetRescueds { get; set; }

    public VetBills Bills { get; set; }

    public PetBreed PetBreed { get; set; }

    public PetSpecies PetSpecies { get; set; }

    public PetRescuedViewModel()
    {
        PetRescueds = new PetRescued();
    }
}

这是我的PetRescuedController

public ActionResult New()
    {


        var viewModel = new PetRescuedViewModel();

        return View("PetRescued", viewModel);
    }

    [HttpPost]
    public ActionResult Save(PetRescued petRescued)
    {
        if (petRescued.Id == 0)
            _context.PetRescueds.Add(petRescued);
        else
        {
            var petRescuedInDb = _context.PetRescueds.Single(c => c.Id == petRescued.Id);
            petRescuedInDb.PetName = petRescued.PetName;
            petRescuedInDb.PetSpecies = petRescued.PetSpecies;
            petRescuedInDb.PetAge  = petRescued.PetAge;
            petRescuedInDb.PetGender  = petRescued.PetGender;
            petRescuedInDb.PetWeightInKg  = petRescued.PetWeightInKg;
            petRescuedInDb.DateWhenRescued  = petRescued.DateWhenRescued;
        }

        _context.SaveChanges();
        return RedirectToAction("Index", "PetRescued");
    }

这是我的PetRescuedForm,它是SelectList

<div class="form-group">
    @Html.LabelFor(m => m.PetRescueds.PetSpecies)
    @Html.DropDownListFor(m => m.PetRescueds.PetSpecies.Id, new SelectList(Model.PetSpecies.SpeciesName, "Id", "SpeciesName"), "Select A Species", new {@class = "form-control"})    
</div>

0 个答案:

没有答案