How do you write a get method that retrieves the title of an object, not its Id - w/ ASP.NET MVC
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);
}
精彩评论