我在MVC应用程序中遇到了奇怪的问题。
我有这样的页面导航链接 -
<a href="/Home/CardDetails?cardID=@Model.cardID">@Model.cardTitle</a>
所以我改变了它的路线以使其成为规范并在route.config
文件中添加了一个条目&#34;默认&#34;路线 -
routes.MapRoute(
name: "HomeCardDetails",
url: "{controller}/{action}/{cardID}/{cardTitle}",
defaults: new { controller = "Home", action = "CardDetails", cardTitle = UrlParameter.Optional }
);
然后根据路线修改链接为 -
<a href="/Home/cardDetails/@Model.cardID/@Model.cardTitle">@Model.cardTitle</a>
控制器方法 -
public ActionResult CardDetails(CardModel card) {
var cardDetail = (from u in db.CardTables
where u.IsApproved == "YES" && u.CardID == card.cardID
join v in db.FunRegistereds
on u.FKCardID equals v.UserID
select new CardModel {
cardID = u.CardID,
cardHashCode = u.CardHashCode,
cardDate = u.CardDate,
cardFileName = u.CardFileName,
cardFilePath = u.CardFilePath,
cardTitle = u.CardTitle.Replace(" ","-"),
fkcardID = Convert.ToInt32(u.FKCardID),
aboutCard = u.AboutCard,
uploadedBy = u.UploadedBy == null ? "Anonymous" : u.UploadedBy,
cardspamcount = u.CardSpams == null ? 0 : Convert.ToInt32(u.CardSpams),
cardfavoritecount = u.CardFavorites == null ? 0 : Convert.ToInt32(u.CardFavorites),
cardlovecount = u.CardLoves == null ? 0 : Convert.ToInt32(u.CardLoves),
cardhatecount = u.CardHates == null ? 0 : Convert.ToInt32(u.CardHates)
}).SingleOrDefault();
if (Request.IsAuthenticated && Session["LoggedInUser"] != null) {
if (cardDetail.fkcardID == this.LoggedInUser.UserID) {
ViewBag.Username = cardDetail.uploadedBy;
}
return View(cardDetail);
}
else {
return View(cardDetail);
}
}
现在这条新路线似乎运作良好。但View现在永远不会出现布局。
它呈现没有布局的页面。
我的actionResult是类型View
,它在呈现时添加了viewstart.cshtml
的条目。
我只是想知道为什么会发生这种情况以及两者之间发生了什么?
答案 0 :(得分:0)
您的布局应在View“CardDetails”之上声明,其中“CardDetails”的型号为“CardModel”。
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
显示完整路线和View文件的开头。 为什么在路线中使用{cardTitle}? 它适用于:
routes.MapRoute(
name: "HomeCardDetails",
url: "{controller}/{action}/{cardID}",
defaults: new { controller = "Home", action = "CardDetails", cardID= UrlParameter.Optional }
);