behnam farhadi

behnam farhadi

  • NA
  • 158
  • 12.8k

why ActionResult method parameter is null in asp.net MVC?

May 20 2017 10:02 AM
I'm trying to send a form to ActionResult method but it is null always.
In fact, I got the error Value cannot be null. but I don't know why I got it the error.

Here are ActionResult code, my view, and model.
 
 
  1. public class VocabularyController : Controller  
  2. {  
  3.     private VocabContext _context;  
  4.    
  5.     public VocabularyController()  
  6.     {  
  7.         _context = new VocabContext();  
  8.     }  
  9.     // GET: Vocabulary  
  10.     [Route("New")]  
  11.     public ActionResult New()  
  12.     {  
  13.         return View();  
  14.     }  
  15.    
  16.     [HttpPost]  
  17.     public ActionResult Save(Vocabulary word)  
  18.     {  
  19.    
  20.         if (ModelState.IsValid)  
  21.         {  
  22.             _context.Vocabularies.Add(word);  
  23.             _context.SaveChanges();  
  24.         }  
  25.    
  26.         return RedirectToAction("dashboard""Home");  
  27.     }  
  28.    
  29.   
  30.    
  31. }  
  32. ==============================  
  33.    
  34. @model EnglishTest.Models.Vocabulary  
  35.    
  36. @{  
  37.     ViewBag.Title = "New";  
  38. }  
  39.    
  40. <div class="row">  
  41.     <div class="col-lg-12">  
  42.         <div class="element-wrapper">  
  43.             <h6 class="element-header">New Word Form</h6>  
  44.             <div class="element-box">  
  45.                 @using (Html.BeginForm("Save""Vocabulary", FormMethod.Post))  
  46.                 {  
  47.                 <div class="form-group">  
  48.                     @Html.LabelFor(m => m.Word)  
  49.                     @Html.TextAreaFor(m => m.Word, new { @class = "form-control", @placeholder = "Word" })  
  50.                     @Html.ValidationMessageFor(m => m.Word)  
  51.                 </div>  
  52.                 <div class="row">  
  53.                     <div class="col-sm-12">  
  54.                         <div class="form-group">  
  55.                             @Html.LabelFor(m => m.Defination)  
  56.                             @Html.TextAreaFor(m => m.Defination, new { @class = "form-control", @placeholder = "Definition" })  
  57.                             @Html.ValidationMessageFor(m => m.Defination)  
  58.                         </div>  
  59.    
  60.                     </div>  
  61.                 </div>  
  62.                 <div class="row">  
  63.                     <div class="col-sm-12">  
  64.                         <div class="form-group">  
  65.                             @Html.LabelFor(m => m.Synonym)  
  66.                             @Html.TextAreaFor(m => m.Synonym, new { @class = "form-control", @placeholder = "Synonym" })  
  67.                             @Html.ValidationMessageFor(m => m.Synonym)  
  68.                         </div>  
  69.                     </div>  
  70.                 </div>  
  71.    
  72.                 <div class="row">  
  73.                     <div class="col-sm-12">  
  74.                         <div class="form-group">  
  75.                             @Html.LabelFor(m => m.PersianTranslate)  
  76.                             @Html.TextAreaFor(m => m.PersianTranslate, new { @class = "form-control", @placeholder = "Persian Translation" })  
  77.                             @Html.ValidationMessageFor(m => m.PersianTranslate)  
  78.                         </div>  
  79.                     </div>  
  80.                 </div>  
  81.    
  82.                 <div class="row">  
  83.                     <div class="col-sm-12">  
  84.                         <div class="form-group">  
  85.                             @Html.LabelFor(m => m.Examples)  
  86.                             @Html.TextAreaFor(m => m.Examples, new { @class = "form-control", @placeholder = "Examples" })  
  87.                             @Html.ValidationMessageFor(m => m.Examples)  
  88.                         </div>  
  89.                     </div>  
  90.                 </div>  
  91.                 @Html.HiddenFor(m => m.Id)  
  92.                 <div class="form-buttons-w"><button class="btn btn-primary" type="submit"> Save</button></div>  
  93.             }  
  94.         </div>  
  95.     </div>  
  96.     </div>  
  97. </div></div>  
  98. ==============================  
  99.    
  100.    public class Vocabulary  
  101.     {  
  102.    
  103.         public int Id { getset; }  
  104.         [Required]  
  105.         public string Word { getset; }  
  106.         [Required]  
  107.         public string Defination { getset; }  
  108.         [Required]  
  109.         public string Synonym { getset; }  
  110.         [Required]  
  111.         public string PersianTranslate { getset; }  
  112.         [Required]  
  113.         public string Examples { getset; }  
  114.     }  
 

Answers (1)