0
在我的項目中,有兩個控制器,即家庭控制器和酒店控制器。我用視圖模型來組合兩個模型類。波紋管我添加了我的控制器。ID值未傳遞給不同的動作方法
家庭控制器
public ActionResult Index(){
List<ImageData> details = new List<ImageData>();
var sp_details = (from s in db.service_provider
join p in db.pictures on s.SPID equals p.SPID
join c in db.cities on s.City_ID equals c.City_ID
select new { s.SPID, s.Sp_name, s.Sp_rate, s.service_type, c.Cityname, p.pic }).OrderByDescending(s => s.Sp_rate).Where(p => p.service_type == "Restaurant").Take(3).ToList();
foreach (var item in sp_details)
{
ImageData SpView = new ImageData(); // ViewModel
SpView.SPID = item.SPID;
SpView.Sp_name = item.Sp_name;
SpView.Cityname = item.Cityname;
SpView.Sp_rate = item.Sp_rate;
SpView.pic = item.pic;
details.Add(SpView);
}
return View(details);
}
酒店控制器
public ActionResult Details(int id = 0)
{
List<ImageData> details = new List<ImageData>();
var sp_details = (from s in db.service_provider
join p in db.pictures on s.SPID equals p.SPID
join c in db.cities on s.City_ID equals c.City_ID
where s.SPID == id
select new ImageData()
{
Sp_name = s.Sp_name,
Sp_location = s.Sp_location,
Cityname = c.Cityname,
service_type = s.service_type,
Sp_description = s.Sp_description,
Sp_rate = s.Sp_rate,
Sp_web = s.Sp_web,
Cnt_wh = s.Cnt_wh,
pic = p.pic
});
if (details == null)
{
return HttpNotFound();
}
return View(sp_details);
}
下面是已經過去了的價值在酒店控制器詳細操作方法ID索引view.I的一部分。不過去了,不顯示酒店Controller.Since詳細操作方法的細節
<p class="name">@Html.ActionLink(item.Sp_name, "Details","Hotel", new { id = item.SPID })</p>
但值我是新來的MVC 4我能不能發現其中的錯誤。
只是想知道,如果操作系統的詳細信息的ActionResult的參數已經初始化..'公共ActionResult詳細信息(int id = 0)',他的行程鏈接中的routeValue是不是毫無意義?或者OP的routeValue * override * int id = 0?' –
int id = 0意味着默認參數的值爲0.所以如果提供了一個'id'值,它會覆蓋它,如果不是,仍然會使用0。 –
啊,這是一個*可選參數*? –