2013-04-22 85 views
1

我想做一個C#ASP.NET MVC4網站。我真的不確定我的控制器文件是如何佈置的。下面是該文件的外觀:Asp.NET MVC4 - 「這些去哪裏?」

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 
} 

看來,「一些類」和「某些功能」是格格不入這裏,但我不知道還有什麼地方放了。有什麼建議?讓我知道它是否有助於粘貼整個文件!我認爲上述會更容易。

編輯:謝謝你的見解!所以,我能得到這個確定下來,這裏的類(即你已經指出了自己的CS文件應該去):

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

和功能如下所示:

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

所以現在如果我能得到一些關於這些應該去哪裏的反饋,我會很感激。

+3

實際上,而不是*僞代碼*,實際的代碼更有用。堆棧溢出在查看代碼/具體事物方面蓬勃發展。不要粘貼整個**文件,而要粘貼你認爲重要的部分。一旦熟悉ASP.NET MVC領域的用戶看到它,他們將能夠爲您提供更好的反饋 - 例如想法和/或要求更多代碼。 – Jesse 2013-04-22 01:38:37

+0

一般來說,類都有自己的.cs文件,並放置在MVC的模型文件夾中。我們可能需要查看他們的「某些功能」以確定「正確」的位置。我猜測只要控制器是使用它們的唯一東西,它們可能就位於那裏。 – Shelby115 2013-04-22 02:45:11

回答

1

我不完全確定你在找什麼。看起來你正在黑暗中尋找一些指導,所以我會從那裏開始。

MVC.NET有一些非常有用的約定,你很快就會通過看像this微軟的一些教程學習他們。 Best Practices也有很多意見。

的根本出發點是定義你的控制器,視圖和模型(每一類又都在同一個名字的文件夾,按照約定)。默認的MVC模板將有一個HomeController和相關的視圖。 Scott Gu爲MVC2提供了一些有效的帖子,如routing

但是你決定建立MVC,記住,你的業務邏輯是在不同的層次,所以你的控制器將調用其他地方的業務代碼,這將調用數據庫訪問代碼另一層。

+0

感謝您的指點。我會檢查這些資源。 – user2305673 2013-04-23 12:41:42