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.
- public class VocabularyController : Controller
- {
- private VocabContext _context;
-
- public VocabularyController()
- {
- _context = new VocabContext();
- }
-
- [Route("New")]
- public ActionResult New()
- {
- return View();
- }
-
- [HttpPost]
- public ActionResult Save(Vocabulary word)
- {
-
- if (ModelState.IsValid)
- {
- _context.Vocabularies.Add(word);
- _context.SaveChanges();
- }
-
- return RedirectToAction("dashboard", "Home");
- }
-
-
-
- }
- ==============================
-
- @model EnglishTest.Models.Vocabulary
-
- @{
- ViewBag.Title = "New";
- }
-
- <div class="row">
- <div class="col-lg-12">
- <div class="element-wrapper">
- <h6 class="element-header">New Word Form</h6>
- <div class="element-box">
- @using (Html.BeginForm("Save", "Vocabulary", FormMethod.Post))
- {
- <div class="form-group">
- @Html.LabelFor(m => m.Word)
- @Html.TextAreaFor(m => m.Word, new { @class = "form-control", @placeholder = "Word" })
- @Html.ValidationMessageFor(m => m.Word)
- </div>
- <div class="row">
- <div class="col-sm-12">
- <div class="form-group">
- @Html.LabelFor(m => m.Defination)
- @Html.TextAreaFor(m => m.Defination, new { @class = "form-control", @placeholder = "Definition" })
- @Html.ValidationMessageFor(m => m.Defination)
- </div>
-
- </div>
- </div>
- <div class="row">
- <div class="col-sm-12">
- <div class="form-group">
- @Html.LabelFor(m => m.Synonym)
- @Html.TextAreaFor(m => m.Synonym, new { @class = "form-control", @placeholder = "Synonym" })
- @Html.ValidationMessageFor(m => m.Synonym)
- </div>
- </div>
- </div>
-
- <div class="row">
- <div class="col-sm-12">
- <div class="form-group">
- @Html.LabelFor(m => m.PersianTranslate)
- @Html.TextAreaFor(m => m.PersianTranslate, new { @class = "form-control", @placeholder = "Persian Translation" })
- @Html.ValidationMessageFor(m => m.PersianTranslate)
- </div>
- </div>
- </div>
-
- <div class="row">
- <div class="col-sm-12">
- <div class="form-group">
- @Html.LabelFor(m => m.Examples)
- @Html.TextAreaFor(m => m.Examples, new { @class = "form-control", @placeholder = "Examples" })
- @Html.ValidationMessageFor(m => m.Examples)
- </div>
- </div>
- </div>
- @Html.HiddenFor(m => m.Id)
- <div class="form-buttons-w"><button class="btn btn-primary" type="submit"> Save</button></div>
- }
- </div>
- </div>
- </div>
- </div></div>
- ==============================
-
- public class Vocabulary
- {
-
- public int Id { get; set; }
- [Required]
- public string Word { get; set; }
- [Required]
- public string Defination { get; set; }
- [Required]
- public string Synonym { get; set; }
- [Required]
- public string PersianTranslate { get; set; }
- [Required]
- public string Examples { get; set; }
- }