Question

I'm trying to make a C# ASP.NET MVC4 site. I'm really unsure of how I have my controllers file laid out. Here's how the file looks:

namespace{
    *Some Classes that'll be used by the controllers
    *Controller
    *ActionResult Index(parameter)
    *IndexViewModel
    *Some functions (methods in asp.net?) that make database calls and bundle up
     the content to be sent to the view
}

It seems that "some classes" and "some functions" are out of place here but I'm not sure where else to put them. Any advice? Let me know if it'd help to paste the entire file! I thought the above would be easier.

Edit: Thank you both for the insight! So I can get this nailed down, here's the classes (that you've already pointed out should go in their own cs file):

public class contentObject
{
    //The HTML is the content that'll be rendered on the page.
    public string theHTML { get; set; }
    //The title is the content's title.
    public string theTitle { get; set; }
}
public class utilityItem
{
    //the menu is the user-specific menu that'll be constructed
    public string menu { get; set; }
    //notes is the information about the user/company that might assist the
    //tech support people
    public string notes { get; set; }
    //notice is company-specific text that allows us to communicate with the clients.
    public string notice { get; set; }
    //companyName is the company name that'll be displayed
    public string companyName { get; set; }
}
public class listItem
{
    /* listItem is a single entry in the menu will be used to construct the entire menu, 
    which eventually gets bundled into utilityItem.*/
    public int theID { get; set; }
    public string theTitle { get; set; }
}

and the functions look like this:

    public contentObject buildContent(int TopicID, int userID, HelpSiteEntities1 db)
    {
        var queryX = (from H in db.HelpTopics
                      where H.ID == TopicID
                      select new contentObject() { theHTML = H.HTML, theTitle = H.TITLE }).ToArray();
        return queryX[0];
    }
    public utilityItem buildUtility(int userID, HelpSiteEntities1 db)
    {
        var menu = "";
        var queryMenuItems = (from H in db.HelpTopics
                              join P in db.HelpTopicMaps
                              on H.ID equals P.TopicID
                              where P.UserID == userID
                              select new { theID = P.TopicID, theTitle = H.TITLE }).ToList();
        var queryVisitorInfo = (from U in db.Users
                                where U.ID == userID
                                select new { notes = U.SpecificNotes, notice = U.Notice, companyName=U.CompanyName }).ToList();
        int ittrvar = 0;
        foreach (var item in queryMenuItems)
        {
            menu += "<li><a href='/HelpTopic/Index?TopicID=" + queryMenuItems[ittrvar].theID + "'>" + queryMenuItems[ittrvar].theTitle + "</a></li>";
            ittrvar++;
        }
        var utility = new utilityItem { menu = menu, notes = queryVisitorInfo[0].notes, notice = queryVisitorInfo[0].notice, companyName=queryVisitorInfo[0].companyName };
        return utility;
    }
}

So now if I could get some feedback on where these should go I'd appreciate it.

Was it helpful?

Solution

I'm not entirely sure what you're seeking. It looks like you're casting in the darkness looking for a bit of guidance, so I'll start there.

MVC.NET has some very useful conventions and you will quickly learn them by looking at a few tutorials like this from Microsoft. There are many opinions on Best Practices, too.

The fundamental starting point is to define your Controllers, Views, and Models (each of those are in folders of the same name by convention). A default MVC template will have a HomeController and associated views. Scott Gu had several good posts for MVC2 that are still valid, like this one on routing.

However you decide to set up the MVC, remember that your business logic goes in a different tier, so your controller will call business code elsewhere, which will call database access code in another tier.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top