2016-12-20 52 views
-1

我有一個事件與顯示事件視圖有關的信息。在這個觀點上,我試着去參加與喜歡類似的功夫,但我沒有什麼問題。我的邏輯很簡單,我想保持它:點擊,它會增加int參加1,但...ASP.NET MVC:Attends /喜歡發貼

首先我進入if(events == null)...第二是有沒有辦法讓每個用戶點擊一次?

這裏是我的代碼:

型號:

[Key] 
    public int Id { get; set; } 
    public string EventName { get; set; } 
    ... 
    public int Attends { get; set; } 

事件創建操作:

//POST: Event/Create 
    [HttpPost] 
    public ActionResult Create(EventViewModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      using (var database = new EventSpotDbContext()) 
      { 
       var events = new Event(model.EventName,...); 
       ... 
       events.Attends = 0; 
       database.Events.Add(events); 
       database.SaveChanges(); 
       return RedirectToAction("Main"); 
      } 
     } 
     return View(model); 
    } 

事件參加行動:

public ActionResult Attend(int? id) 
    { 
     using (var database = new EventSpotDbContext()) 
     { 
      var events = database.Events.FirstOrDefault(a => a.Id == id); 
      if (events == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
      events.Attends += 1; 
      database.SaveChanges(); 
     } 
     return RedirectToAction("Details"); 
    } 

事件詳細操作:

//GET: Event/Details 
    public ActionResult Details(int? id) 
    { 
     using (var database = new EventSpotDbContext()) 
     { 
      var events = ... .First(); 
      return View(events); 
     } 
    } 

詳細查看

  @model EventSpot.Models.Event 
      ... 
      @Html.ActionLink("Attend Event", "Attend", "EventController", new {@id=Model.Id}) 
      @Model.Attends 
+0

你的問題是在ISSE?從你聲明中不清楚 –

+0

我進入了if(events == null)異常 - HTTP錯誤400.0 - 錯誤請求 –

+0

你有沒有調試過它?用戶界面(視圖)真的傳遞了ID和你的數據庫的作品? –

回答

0
@Html.ActionLink("Attend Event", "Attend", "EventController", new {@id=Model.Id}, null) 

,他與{@id=Model.Id}處理的HTML屬性沒有路由屬性