Click here to Skip to main content
15,851,514 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
public class VocabularyController : Controller
{
    private VocabContext _context;

    public VocabularyController()
    {
        _context = new VocabContext();
    }
    // GET: Vocabulary
    [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; }
    }


What I have tried:

I wanna send a form to an actionResutl method in asp.net MVC.
Posted
Updated 20-May-17 7:48am

1 solution

Hi,

I would try changing m => m. to model => model.
C#
@Html.TextAreaFor(m => m.Word, new { @class = "form-control", @placeholder = "Word" })
@Html.ValidationMessageFor(m => m.Word

Such as:
C#
@Html.TextAreaFor(model => model.Word, new { @class = "form-control", @placeholder = "Word" })
@Html.ValidationMessageFor(model => model.Word


And
C#
public ActionResult Save(Vocabulary word)

to
C#
public ActionResult Save(Vocabulary model


Also as you can see from this article
Getting Data From View to Controller in MVC[^]

Note that most likely the model => is the name expected in the controller action...but I'm not sure if this is actually how mvc works although it should post values this way.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900