【发布时间】: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