2014-10-03 1 views
1

Iam 해결해야 할 문제가 있습니다.매개 변수 사전에는 null이 아닌 'System.Int32'형식의 매개 변수 'userId'에 대한 null 항목이 있습니다.

"매개 변수 사전에 nullable 형식의 매개 변수 'userId'에 대한 null 항목이 포함되어 있습니다. 'System.Windows.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); 
} 
+1

'Edit()'뿐만 아니라'Index()'컨트롤러 메소드를 게시 할 수 있습니까? 문제가 거기에있는 것 같습니다. – Rhumborl

+0

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 <= endTime) .Where (s => s.UserId == userId) .ToList(); (stampings.Count == 0) } 반환보기 (stampings); } 불가능합니다! – Qvadriologic

답변

2

검지 방법은 parmeters (int userId, int year, int weekNo) 기대 당신은 그래서 널 여기 반환 paramerters return RedirectToAction("Index");없이 여기를 호출

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

사용자 ID는 색인 방법에 따라서 데이터가 스탬핑에 반환되지 않으며 당신이 전달되지 않기 때문에 그것을보기 위해 건네줍니다.

+0

그래서 변경해야합니다 : return RedirectToAction ("Index", new {userId = stamping.UserId, year = stamping.Timestamp.Year, weekNo = stamping.Timestamp.DayOfWeek}); 맞습니까? – Qvadriologic

+0

인덱스 메소드가 매개 변수를 예상하기 때문에 정확하게 그렇습니다. – Mairaj

관련 문제