【问题标题】:The parameters dictionary contains a null entry for parameter 'userId' of non-nullable type 'System.Int32' for method参数字典包含方法的不可空类型“System.Int32”的参数“userId”的空条目
【发布时间】:2014-10-03 08:51:14
【问题描述】:

我有一个小问题需要解决。

我在尝试编辑时收到此错误:“参数字典包含方法'System.Web.Mvc.ActionResult Index(Int32, Int32) 的不可空类型'System.Int32'的参数'userId'的空条目, Int32)"

型号:

public partial class Stamping
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Required]
    public int UserId { get; set; }

    [Required]
    [DataType(DataType.DateTime)]
    public DateTime Timestamp { get; set; }

    [Required]
    [StringLength(3)]
    public string StampingType { get; set; }

    public virtual User User { get; set; }
}

查看:

@model Aviato.Models.Stamping

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Redigera</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        @Html.HiddenFor(model => model.Id)
        @Html.HiddenFor(model => model.UserId)

        <div class="form-group">
            @Html.LabelFor(model => model.Timestamp, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Timestamp, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Timestamp, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.StampingType, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.StampingType, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.StampingType, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Spara" class="btn btn-default" />
            </div>
        </div>
    </div>
}

控制器:

public ActionResult Edit(int? id)
{
   var stamping = _db.Stampings.Find(id);

   return View(stamping);
}

[HttpPost]
public ActionResult Edit(Stamping stamping)
{
   if (ModelState.IsValid)
   {
       _db.Entry(stamping).State = EntityState.Modified;
       _db.SaveChanges();
       return RedirectToAction("Index");
   }

   return View(stamping);
}

public ActionResult Index(int userId, int year, int weekNo) 
{
  var startTime = DateConverter.FirstDateOfWeek(year, weekNo); 
  var endTime = startTime.AddDays(6); 
  const string msg = "Stampings not found."; 
  var stampings = _db.Stampings.Where(s => s.Timestamp >= startTime && s.Timestamp <= endTime)  .Where(s => s.UserId == userId).ToList(); 
  if (stampings.Count == 0) 
  { 
    return RedirectToAction("GetWeekStamp", new {message = msg}); 
  } 
  return View(stampings); 
}

【问题讨论】:

  • 你能发布Index()控制器方法,而不仅仅是Edit()。问题似乎就在那里。
  • public ActionResult Index(int userId, int year, int weekNo) { var startTime = DateConverter.FirstDateOfWeek(year, weekNo); var endTime = startTime.AddDays(6); const string msg = "未找到冲压件。"; var stampings = _db.Stampings.Where(s => s.Timestamp >= startTime && s.Timestamp s.UserId == userId).ToList(); if (stampings.Count == 0) { return RedirectToAction("GetWeekStamp", new {message = msg}); } 返回视图(冲压件); } 这不可能!

标签: c# .net asp.net-mvc entity-framework dictionary


【解决方案1】:

您的索引方法需要参数 (int userId, int year, int weekNo),而您在这里调用它时没有参数 return RedirectToAction("Index");,所以它在这里返回 null

var stampings = _db.Stampings.Where(s => s.Timestamp >= startTime && s.Timestamp <= endTime)  .Where(s => s.UserId == userId).ToList(); 

因为 userid 未传递给 Index 方法,因此在标记中没有返回数据,您将其传递给查看。

【讨论】:

  • 所以我需要更改: return RedirectToAction("Index", new {userId = stamping.UserId, year = stamping.Timestamp.Year, weekNo = stamping.Timestamp.DayOfWeek});对吗?
  • 是的,因为 index 方法需要参数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-30
  • 1970-01-01
  • 2013-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多