1

I have been asking a lot of questions about creating routes and creating new pages and so on and so forth from a data base, I have the following model, control and the view below:

Model

namespace LocApp.Models
{
    public class ContentManagement
    {
        public int id { get; set; }
        [Required]
        public string title { get; set; }
        public string content { get; set; }
    }
}

Controller

    public ViewResult Index(string title)
    {
        using (var db = new LocAppContext())
        {
            var content = (from c in db.Contents
                           where c.title == title
                           select c).ToList();

            return View(content);

        }
    }

View (partial)

@model IEnumerable<LocApp.Models.ContentManagement>

@{
    foreach (var item in Model)
    {
       <h2>@item.title</h2>
        <p>@item.content</p> 
    }
}

*View (Full) - Note that this code calls the _Content partial*

@model IEnumerable<LocApp.Models.ContentManagement>

@{
    ViewBag.Title = "Index";
}

@{
    if(HttpContext.Current.User.Identity.IsAuthenticated)
    {
        <h2>Content Manager</h2>

        Html.Partial("_ContentManager");
    }
    else
    {
        Html.Partial("_Content");
    }
}

When you go to site.com/bla the model is processed and contains information, but then it "magically" reloads, I break pointed through the controller and the view to watch this happen. On the second time the model is empty thus no content is displayed on the page.

My routes look s follows:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("favicon.ico");

        routes.MapRoute(
            "ContentManagement",
            "{title}",
            new { controller = "ContentManagement", action = "Index", title = UrlParameter.Optional }
        );

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );


    }

Update

It appears the issue is simple: the index is taking in a title, looping through and getting the contents, it then passes that to view like it should. but before the page is finished loading it loops through again, this time passing null as the title and thus loading an empty page.

TheWebs
  • 12,470
  • 30
  • 107
  • 211
  • Nothing in your code looks incorrect. Watch the requests in fiddler and see what is happening. – BNL Apr 25 '13 at 17:52
  • Is this EF Code First? Does the linq query return the correct type for the view? List not List. I think you will find it is the latter. – BNL Apr 25 '13 at 17:56
  • Nothing wrong with what you've posted. Any Javascript calls to `window.location.reload` or similar that could be getting fired by accident? Any AJAX calls in your view or layout? – Ant P Apr 25 '13 at 18:03
  • Nothing, when I step through I see that it passes the title to the index action as "bla" then it loops through everything again and passes "null" in and then the page loads. – TheWebs Apr 25 '13 at 18:07
  • What is the type of the `content` variable passed into the view? – BNL Apr 25 '13 at 18:08
  • is this partial view or complete/full view? Paste your complete view markup code. – Kundan Singh Chouhan Apr 25 '13 at 18:09
  • @KundanSinghChouhan Please read the full post, that was the **partial**, the **full** is posted. please make sure to carefully read. Also The content object is of type ContentManagement, so when ti loads the first time you get title of bla and content of what ever, when it loops the second time the model is empty, the issue here is that it is looping twice and I dont know why, there is only one object being passed in and queried for. – TheWebs Apr 25 '13 at 18:15
  • You are sure the content object is `List` not `List`? Are there multiple requests being made to the web server or is the loop within the same request? – BNL Apr 25 '13 at 18:19
  • there is only one model called ContentManagement and only one controller called ContentManagement, the view partial was named content in order for non logged in users to see non admin jazz, that has nothing to do this issue though – TheWebs Apr 25 '13 at 18:21
  • what maps the linq object db.Contents to the ContentManagement object? – BNL Apr 25 '13 at 18:23
  • I think you are supposed to be seeing an error page but the request for the error page is being trapped by your "ContentManagement" route. Can you try taking the "ContentManagement" route out of the equation and loading the controller action directly with the default route? – BNL Apr 25 '13 at 18:25
  • @BNL this is getting to long, please post an answer and to answer yuor first question: `public DbSet Contents { get; set; }` thats why you get `db.Contents`. If I take it out (the route), then I get "no such resource /bla" error page. – TheWebs Apr 25 '13 at 18:26
  • Thus why I asked in the second comment if you were using Code First. I don't have an answer yet to post. You can request the page without the /bla route just type /ContentMangement/Index?title=text – BNL Apr 25 '13 at 18:28
  • @BNL - I have to request it via the route I have using `site.com/bla` it cannot be any other way. The problem, as stated is that it loops through everything twice, once - all the content information is there, but then it loops through again passing null as the title and thus the page loads empty. why is it looping twice? – TheWebs Apr 25 '13 at 18:31
  • I understand that is what you need to do, I'm trying to determine if the route is what is causing your problem. Nothing else here is. That route is going to catch any requests that come in, probably including any errors. You should verify the code works without the route. Other than that, I don't have any suggestions, so good luck. – BNL Apr 25 '13 at 18:35
  • It doesnt work I get the error of "no such resource" – TheWebs Apr 25 '13 at 18:36
  • My suggestion is still to make sure you have it working w/o the route, then bring the route back in to the equation. – BNL Apr 25 '13 at 19:28

1 Answers1

-1

The biggest issue that I see is that you are not actually passing your model to your partial views

@model IEnumerable<LocApp.Models.ContentManagement>

@{
    ViewBag.Title = "Index";
}

@{
    if(HttpContext.Current.User.Identity.IsAuthenticated)
    {
        <h2>Content Manager</h2>

        Html.Partial("_ContentManager", Model);
    }
    else
    {
        Html.Partial("_Content", Model);
    }
}
Tommy
  • 39,592
  • 10
  • 90
  • 121
  • This isn't necessary. Partial views inherent the model of their parents, at least when the model types are the same as they are here. http://stackoverflow.com/questions/13431056/in-mvc-do-partial-views-inherit-the-models-of-their-parent-views That question isn't the best, but I just verified this on my PC. – BNL Apr 25 '13 at 19:18
  • Correct - but as stated in the answer, this is not good practice. And given the OP is having issues, this would be the first place I would start. – Tommy Apr 25 '13 at 19:24
  • this is not an issue. The model is being passed – TheWebs Apr 25 '13 at 19:55