I've started learning .NET with MVC 4 in C# and I built a basic application to display records from a MySQL database. It's incredibly basic, but it's the best I could come up with being a complete beginner to C# and all.
Most of the programming I do is with Ruby on Rails and I tried to carry over some of the design patterns that I use there. I don't know the .NET conventions so if you see anything that should be changed please let me know.
Also, I don't fully understand how to find records with LINQ and pass them to the view. I ended up getting lucky and finding an article that somewhat explained it, but I don't have a firm grasp on the subject. If you know of any tutorials or articles that could shed some light on the subject I'd be very grateful.
P.S. The naming conventions for my entities and models are wrong. It's a long story, basically it took me a while to connect to my MySQL database and when I generated them I didn't care how they were named.
Here's the code I would like ya'll to look at.
RouteConfig.cs
routes.MapRoute(
name: "root",
url: "",
defaults: new { controller = "Authors", action = "Index" }
);
routes.MapRoute(
name: "author_books",
url: "authors/{author_id}/books",
defaults: new { controller = "Authors", action = "Books" }
);
AuthorsController.cs
public class AuthorsController : Controller {
bookisticsEntities be = new bookisticsEntities();
//
// GET: /Authors/
public ActionResult Index() {
return View(be.authors.ToList());
}
public ActionResult Books(int author_id = 0) {
author aut = be.authors.Find(author_id);
var books = from b in be.books
join a in be.authors
on b.author_id equals a.id
where b.author_id == author_id
select b;
ViewBag.author_name = aut.name;
return View(books.ToList());
}
}
I've omitted the view files because I pretty much left them at default after they were generated.