首先,我同意@Steve :)。但是,如果你真的想用
int? id
,你可以查一下你的控制器的方法如果ID使用簡單
if(id == null)
如果是這樣,加載從數據庫中的所有物品(或東西設置並且將這些傳遞給您的視圖(直接或通過使用視圖模型)。如果設置了id,只需從數據庫中加載具有該id的文章並將其發送到視圖(如果不使用視圖模型,則可能在列表中)。
在您的視圖中,只是加載列表中的所有文章與提供給視圖的文章。其中包含全部或僅包含一個。
完全啞代碼
public ActionResult showArticles(int? id){
List<Articles> aList;
if(id == null){
aList = repository.GetAllArticles().ToList();
}else{
aList = new List<Articles>();
aList.add(repository.GetArticleByID(id));
}
return View(aList);
}
你的觀點有類似:
<% Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<List<Articles>>"%>
foreach(Articles a in Model)
//display article
而且你怎麼稱呼它使用接下來的兩個選項之一:
html.ActionLink("one article","showArticles","Articles",new {id = aID},null}
html.ActionLink("all articles","showArticles","Articles"}
」或者將其移動到Articles控制器(使用默認路徑的網址將是:Articles and Articles/Detail/{id}):「 這可能是最好的方法。謝謝! – 2010-10-05 17:28:26