Hi,
I have a viewmodel that contains a product and SelectList of categories.
public class AdFormViewModel
{
public AmericanAds.Model.Ad Ad { get; set; }
public SelectList Categories { get; set; }
public AdFormViewModel(AmericanAds.Model.Ad ad, SelectList categories)
{
Ad = ad;
Categories = categories;
}
}
When adding a new product, if validation fails for category dropdown I get below error message.
The model item passed into the dictionary is of type 'AmericanAds.Model.Ad' but this dictionary requires a model item of type 'AmericanAds.Controllers.AdFormViewModel'.
Here is the controller for create action.
public ActionResult Create()
{
AdFormViewModel data = new AdFormViewModel(
null,
new SelectList(_repository.CategoryList().ToList(), "CategoryId", "CategoryName")
);
return View(data);
}
//
// POST: /Ad/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Ad ad)
{
if (ModelState.IsValid)
{
try
{
_repository.AddAd(ad);
return RedirectToAction("Index");
}
catch
{
return View(ad);
}
}
else
{
return View(ad);
}
}
What am I missing?
As you can tell, I am very new to ASP.Net MVC.
Thanks!