views:

38

answers:

1

So this is the method in my repository I use to get records from my db via its Id

    public BlogsTable GetBlogPosts(int id)
    {
        return db.BlogsTables.SingleOrDefault(d => d.Id == id);
    }

And this is my controller using the get method

    public ActionResult Details(int id)
    {
        BlogsTable blogPostDetails = repo.GetBlogPosts(id);

        return View(blogPostDetails);
    }

What I need is to be able to write a get method that allows me to get records from my db by the title, instead of its id. This is what I have now

Also, how to I set the route to use that method? This is what I have now

            routes.MapRoute(
            "Default",                                              
            "{controller}/{action}/{id}",                           
            new { controller = "Blog", action = "Index", id = "" }  
        );
A: 
    public BlogsTable GetBlogPostsByTitle(string title)
    {
        return db.BlogsTables.SingleOrDefault(d => d.Title == title);
    }

    public ActionResult Details(string id)
    {
        BlogsTable blogPostDetails = repo.GetBlogPostsByTitle(id);

        return View(blogPostDetails);
    }
willbt
How would that work in the details method?Because you parse in the id as the parameter not the title
You'll notice I changed the type of Id to string so you can just pass the title as the id argument. So your url becomes /Blog/Details/How To Get By Title
willbt
Well up till now I haven't actually plugged in that code you posted, Ill do it now and report back what my findings are, thanks for your help btw.
Well your above method doesnt work, I get this "Object reference not set to an instance of an object." error.
Where do you get this exception? This will work assuming you have a record with a Title matching what is passed in to the GetBlogPostsByTitle method.
willbt