【问题标题】:form not submit error comes as null entry for parameter in mvc 4表单未提交错误作为 mvc 4 中参数的空条目出现
【发布时间】:2016-06-26 22:03:53
【问题描述】:

我创建了控制器用于在网页上添加评论。下面是我的评论控制器

public ActionResult AddComment(int id=0)
{
    int spid = id;
    return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddComment(comment cmt,int spid)
{
    if (ModelState.IsValid)
    {
        cmt.SPID = spid;
        db.comments.Add(cmt);
        db.SaveChanges();
        return RedirectToAction("Index", "Comment");
    }
    return View(cmt);
}

这是添加评论视图

@model WEB1.Models.comment
@using (Html.BeginForm("AddComment", "Comment"))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>comment</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.cmd_content)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.cmd_content)
            @Html.ValidationMessageFor(model => model.cmd_content)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.t_email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.t_email)
            @Html.ValidationMessageFor(model => model.t_email)
        </div>
            @Html.HiddenFor(model => model.SPID)
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

下面是评论模型

[Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CMT_ID { get; set; }

    private DateTime _date = DateTime.Now;
    public DateTime cmd_ad
    {
        get { return _date; }
        set { _date = value; }
    }
    [Required(ErrorMessage = "Please add Comment before submit")]
    public string cmd_content { get; set; }
    [RegularExpression(@"^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$",
    ErrorMessage = "Please Enter Correct Email Address")]
    public string t_email { get; set; }
    public Nullable<int> SPID { get; set; }

我收到错误消息

'参数字典包含参数'spid'的空条目,该参数'spid'的不可空类型'System.Int32'用于'WEB1中的方法'System.Web.Mvc.ActionResult AddComment(WEB1.Models.comment,Int32)' .Controllers.CommentController'。可选参数必须是引用类型、可空类型或声明为可选参数。 参数名称:parameters'。

我已经调试了我的代码,它没有在 post Action 方法上命中。我该如何解决这个问题。

【问题讨论】:

  • 我修改了我的问题
  • @StephenMuecke 非常感谢。它有效

标签: c# asp.net-mvc asp.net-mvc-4


【解决方案1】:

您没有将模型传递给视图,因此属性SPID 的值在视图中是null(您的@Html.HiddenFor(model =&gt; model.SPID) 正在生成&lt;input type="hidden" name="SPID" .... value="" /&gt;)所以当您提交null 值时发布表格。

更改您的 GET 方法以初始化模型,设置其属性,然后将该模型返回到视图

public ActionResult AddComment(int id = 0)
{
    var model = new comment(){ SPID = id };
    return View(model );
}

您还可以删除 POST 方法中的 int spid 参数,因为该值已绑定到 cmtSPID 属性

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddComment(comment cmt)
{
    if (ModelState.IsValid)
    {
        db.comments.Add(cmt);
        db.SaveChanges();
        return RedirectToAction("Index", "Comment");
    }
    return View(cmt);
}

【讨论】:

    【解决方案2】:

    查看错误消息,它清楚地说明了问题所在。您的 post 方法模型未正确绑定。参数spid 为空,因此在验证模型绑定时失败,尤其是在if (ModelState.IsValid) { 行中

    您的错误信息

    不可为空类型“System.Int32”的参数“spid”的空条目 对于方法'System.Web.Mvc.ActionResult AddComment(WEB1.Models.comment

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-19
      • 2017-09-22
      • 2021-03-23
      • 2016-06-25
      • 2011-07-28
      • 2014-02-25
      • 2018-11-24
      相关资源
      最近更新 更多