为什么在ASP.NET MVC中actionresult方法参数为null? [英] Why actionresult method parameter is null in ASP.NET MVC?

查看:61
本文介绍了为什么在ASP.NET MVC中actionresult方法参数为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将表单发送到ActionResult方法,但它始终为null。

实际上,我得到的错误值不能为null。但我不知道为什么我错了。



这里是ActionResult代码,我的视图和模型。



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();
    }
    // 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; }
    }





我尝试过:



我想将表单发送到asp.net MVC中的actionResutl方法。



What I have tried:

I wanna send a form to an actionResutl method in asp.net MVC.

推荐答案





我会尝试改变m =>米到model =>模型。

Hi,

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



例如:


Such as:

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







And

public ActionResult Save(Vocabulary word)




to

public ActionResult Save(Vocabulary model





另外从本文中可以看出

从MVC中获取数据从视图到控制器 [ ^ ]



请注意,模型很可能= >是控制器动作中预期的名称......但是我不确定这是否真的是mvc的工作方式,尽管它应该以这种方式发布值。



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.


这篇关于为什么在ASP.NET MVC中actionresult方法参数为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆