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.