为什么在asp.net MVC中ActionResult方法参数为null?

时间:2017-05-20 12:40:50

标签: c# asp.net-mvc

我正在尝试将表单发送到ActionResult方法,但它始终为null。
实际上,我得到了错误 Value不能为空。但我不知道为什么我得错了。

以下是ActionResult代码和我的观点。

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; }
    }

2 个答案:

答案 0 :(得分:2)

我将输入参数更改为我的模型名称,它可以正常工作。

public ActionResult Save(Vocabulary vocabulary)

答案 1 :(得分:0)

为表单指定帖子:

@using (Html.BeginForm("Save", "Vocabulary", FormMethod.Post))

同样不需要第二个@

Html.BeginForm("Save", "Vocabulary", FormMethod.Post)