Relation among the Controller and Views in MVC
I am beginner to MVC and ASP.NET, I viewed many articles about the relationship between Models, Views and Controller. When I tried to do some web sample project, I found that the relationship between View and Controller is quite confused to me.
From the above picture, if we want to update the view from the controller, then we need to manipulate the model to update the view.
But what if I want to pass some simple data or messages to display on View? Then I have to build a Model for it? I heard about Viewbag but is it not a good approach or secure to pass data?
I normally use ViewBag
as they are pretty useful for passing simple data.
If you want to pass more complex data, you might have to create a Model or edit the current one.
for example:
<tr id="pesquisa">
<td>
Nome: @Html.TextBox("nome", ViewBag.nome as string, new { @class = "form-control" })
</td>
<td>
NIF: @Html.TextBox("nif", ViewBag.nif as string, new { @class = "form-control" })
</td>
<td>
Contacto: @Html.TextBox("contacto", ViewBag.contacto as string, new { @class = "form-control" })
</td>
<td>
Email: @Html.TextBox("email", ViewBag.email as string, new { @class = "form-control" })
</td>
<td>
<br />
<input type="submit" value="Pesquisar" align="center" class="btn btn-info" />
</td>
</tr>
Here, I use the ViewBag
to hold the string that user typed in Search area after the search was completed, and the page reloaded.
I hope that helps :)
Disclaimer : I think this question is basically the same as an old question: should you use dynamic typing or static typing? There are many proponents on both sides. Personally I prefer static typing unless there is a very good reason to go with dynamic typing.
I think that some confusion comes from the fact that the term "model" might actually reference different things depending on context:
If you look at the ASP.NET MVC implementation from the point of view of the implementation-agnostic MVC pattern, then Model
, ViewBag
/ ViewData
and TempData
together represent "MVC-model" (ie a piece of data passed from MVC-controller to MVC-view). However for practical reasons designers of the ASP.NET MVC decided to split single logical MVC-model into different physical entities. So technically modifying ViewBag
/ ViewData
is also modifying MVC-model. Being proponent of static type checking I prefer to have an explicit model class to using ViewBag
/ ViewData
. IMHO this provides better (type-)safety and makes it easier to read the code: explicit model class becomes a contract between View and Controller.
Note that "domain model" might and often might not be the same as the ASP.NET Model object. If your application is simple, it might be OK to put all your logic into ASP.NET-controllers and use ASP.NET-models as your domain models. However as your application becomes more complicated you probably want to move business logic out of the ASP.NET-controllers to some services for better re-use. Then you may end up with something like MVC-inside-MVC: you have a business-level MVC where M
are domain models, C
are domain logic services and V
- is represented by the whole ASP.NET MVC stack; and then inside the ASP.NET MVC you have their own models, views and controllers. Under such design ASP.NET controller becomes very thin. It maps user input from HTTP; calls some domain-level service that does all the logic and returns some domain model; then the ASP.NET-controller maps that domain model to its own model probably adding some view-specific fields and renders the View.
上一篇: 有人可以为我简单地解释MVC吗?
下一篇: MVC中控制器和视图之间的关系