参数字典包含非可空类型'System.Int32'的参数'userId'的空条目用于方法

时间:2014-10-03 08:51:14

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

我有一点问题需要解决。

我在尝试编辑时遇到此错误:"参数字典包含参数' userId'的空条目。非可空类型的System.Int32' for method' System.Web.Mvc.ActionResult Index(Int32,Int32,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); 
}

1 个答案:

答案 0 :(得分:2)

您的索引方法需要参数(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方法,因此没有在冲压中返回数据,而是将它传递给view。