Bouthaina Tlijani

Bouthaina Tlijani

  • NA
  • 265
  • 23.1k

ASP.NET MVC 5 : System.ArgumentNullException on DropDownList

Jan 3 2018 8:32 AM
Hello everyone, well i'm facing a problem here, i've created an asp.net mvc5 web application and in it i've used a DropDownList , when i trie to create a new module
i receive this exception:'System.ArgumentNullException' occurred in System.Web.Mvc.dll but was not handled in user code
Additional information: Value cannot be null.
 
This is my controller:
 
// GET: Module/Create
public ActionResult Create()
{
List ListAppli = new List()
{
new SelectListItem() {Text="Tablette", Value="Tablette"},
new SelectListItem() {Text="Mobile", Value="Mobile"},
};
ViewBag.ListAppli = ListAppli;
return View();
}
// POST: Module/Create
[HttpPost]
public ActionResult Create(Module module)
{
if (ModelState.IsValid)
{
db.Modules.Add(module);
db.SaveChanges();
var idManager = new IdentityManager();
var success = idManager.CreateRole(module.libelle+" : "+module.application);
return RedirectToAction("Index");
}
return View(module);
}
//And this is my DropDownList 
 
@Html.DropDownListFor(model => model.application, new SelectList(ViewBag.ListAppli, "Value", "Text"))
//And this is the model 
 
namespace AspNetRoleBasedSecurity.Models
{
public class Module
{
[Key]
public int id { get; set; }
[Required(ErrorMessage = "Le code est requis!")]
[Display(Name = "Code")]
public string code { get; set; }
[Required(ErrorMessage = "Le libellé est requis!")]
[Display(Name = "Libelle")]
public string libelle { get; set; }
[Display(Name = "Application")]
public string application { get; set; }
}
}
//Any idea please :D and thnx ^^ 
 

Answers (2)