Which way to construct project and pass the data in MVC3
I am a bit confusled... as I am new to ASP-MVC. So I was following a
tutorial, but they made a ContextDataBase inside a Model class and that
screwed my understanding totally.
I need to take some strings (HTML code in the form of text) from a
database and pass it to the View. I understand that all the logic, DB
communication, etc., should be in the Controller. And from it I can
directly pass the retrieved values to the View, right ? Then why do I need
the Model in my case?
F.e. I have the following methods in my controller:
public class DBadapterController : Controller
{
private string getConnectionString()
{
return
WebConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
}
public string getXMLfromDB()
{
// retrieve the values from the DB and return them as string
}
public String getHTMLfromXML(string xmlFile, string xslFile)
{
//do some stuff here, not relevant
return htmlResult;
}
and then in another Controller I have the methods where to pass this stuff
to the Views:
public class FuncController : Controller
{
public ActionResult Products()
{
ViewBag.Message = // here get the result from the
DBadapterController
return View();
}
My friends says that the Views are based on their Models and through these
models I pass data from the Controller to the View and vise-verso.
Now I am confusled how to construct the things, where to put the
connection with the database and retrieving from it functions, etc.
What I am asking is: what is the best (and simple way, as for newbies) way
to order the project. I.e., where to have the functions that connect to
the DB and retrieve the information from there; should they be in a Model
class or in a Controller. Also, how is best to pass information from the
Controller to a View - is it through the Model or directly? Another thing
- as I also have functions that firstly do some logic with the information
retrieved from the DB and then pass it to the View - should they be in a
separate Controller from the one that is for accessing the DB (if the DB
is gonna be accessed through a controller in first place, but not a
Model)?
No comments:
Post a Comment