1

I am facing strange problem in my MVC application.

I had navigation link of page like this-

<a href="/Home/CardDetails?cardID=@Model.cardID">@Model.cardTitle</a>

So i changed its route to make it canonical and added an entry in route.config file above "Default" route-

 routes.MapRoute(
                name: "HomeCardDetails",
                url: "{controller}/{action}/{cardID}/{cardTitle}",
                defaults: new { controller = "Home", action = "CardDetails", cardTitle = UrlParameter.Optional }
        );

Then modified link according to route as-

<a href="/Home/cardDetails/@Model.cardID/@Model.cardTitle">@Model.cardTitle</a>

Controller method-

   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);
            }

        }

Now this new route seems to work fine. But View never comes with layout now.

It renders page without layout.

My actionResult is type View and it adds entry of viewstart.cshtml while rendering it. I just want to know why this is happening and what happened in between?

user3163213
  • 703
  • 4
  • 15
  • 37

1 Answers1

0

Your layout should be declared on top of the View "CardDetails" which has a model of type "CardModel".

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

Show you full routes and a beginning of your View file. Why do you use {cardTitle} in the route ? Does it work with:

routes.MapRoute(
                name: "HomeCardDetails",
                url: "{controller}/{action}/{cardID}",
                defaults: new { controller = "Home", action = "CardDetails", cardID= UrlParameter.Optional }
        );
Sergey Shabanov
  • 176
  • 2
  • 11
  • I need to show url with title- like somedomain.com/378/My-new-title . So It has to come with title. – user3163213 Aug 04 '14 at 13:28
  • This may help you: http://stackoverflow.com/questions/2174820/how-to-add-page-title-in-url-in-asp-net-mvc-url-generation and: http://stackoverflow.com/questions/677158/stack-overflow-question-routing . Also: http://stackoverflow.com/questions/6055415/adding-id-and-title-to-url-slugs-in-asp-net-mvc – Sergey Shabanov Aug 05 '14 at 01:51