У меня небольшая проблема, которую мне нужно решить.
Я получаю эту ошибку при попытке изменение: "Словарь параметров содержит нулевую запись для параметра" userId "типа, отличного от нуля," System.Int32 "для метода" Индекс System.Web.Mvc.ActionResult "(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);
}
Ваш индексный метод ожидает парметров (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, поэтому данные не возвращаются в штамповки, и вы передаете его для просмотра.
Index()
, а не простоEdit()
. Кажется, проблема в этом.