I couldn't do it using ViewBag and ViewModel at the same time,
but It works now by Only Using ViewModel
I add a Selectlist of Product Groups to ViewModel like this
public class testViewModel
{
public TestProduct TestProduct { get; set; }
public SelectList ProductGroupSelectListItem { get; set; }
public string testJsonString { get; set; }
}
and Changed Create Get action method like this
public ActionResult Create()
{
testViewModel vm = new testViewModel();
vm.ProductGroupSelectListItem = new SelectList(db.TestProductGroups, "ID", "Title");
//ViewBag.TestProductGroupID = new SelectList(db.TestProductGroups, "ID", "Title");
return View(vm);
}
before It was
public ActionResult Create()
{
ViewBag.TestProductGroupID = new SelectList(db.TestProductGroups, "ID", "Title");
return View();
}
also Change Post Create action like below:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "TestProduct,Title,testJsonString")]testViewModel VM)
{
if (ModelState.IsValid)
{
db.TestProducts.Add(VM.TestProduct);
db.SaveChanges();
return RedirectToAction("Index");
}
VM.ProductGroupSelectListItem = new SelectList(db.TestProductGroups, "ID", "Title", VM.TestProduct.TestProductGroupID);
return View(VM);
}