开发者

How do you write a get method that retrieves the title of an object, not its Id - w/ ASP.NET MVC

开发者 https://www.devze.com 2022-12-17 01:38 出处:网络
So this is the method in my repository I use to get records from my db via its Id public BlogsTable GetBlogPosts(int id)

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 i开发者_如何学Pythons 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 = "" }  
        );


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号