First of all, the next thing is not in line with my identity of the rigorous MVC pattern.
In the process of using MVC to do projects, more and more use of the non-rigorous MVC programming.
For example, in the "cshtml" file, write:
@Html. Raw (DBUnitity.helperP.getNav ("/main/index", @ViewBag. Token))
Do page validation
For example, in the "cshtml" file, write:
class="s-r"> @DBUnitity. Helperp.getsjcount ("sj") "gz") </div> Do data acquisition
Of course, this code is not written by me, it is to maintain the former colleague's code. His code ran well, we did not do optimization, rotten (lazy) rotten (lazy) down.
Today, to talk about, is also a kind of non-rigorous MVC design and implementation of ideas.
We have such a page, such as the need to submit data back to the processing, validation, update, but the submitted data related to the database of multiple tables.
If you follow the pattern that I liked before, then you must encapsulate the page model, for example
Public class ViewModel { publicsetget;} Public Set Get ; } }
Public classPersonmodel { Public stringAge {Set;Get; } Public stringOther {Set;Get; } } Public classLoginmodel { Public stringName {Set;Get; } Public stringPSD {Set;Get; } }
Then write this in the page ". cshtml":
@model Mvcbinddatademo.viewmodel@using (Html.BeginForm ()) { <p> = p.loginmodel.name) < /p> <p> = p.loginmodel.psd) </p> <p> = P.personmodel.age) </p> <p> = p.personmodel.other) </p> <input Type="submit" value="login" />}
And finally receive it inside the controller.
[HttpPost] public actionresult Login (ViewModel obj ) {xxx ... return View (); }
//=========================================================================
But maybe we can write that, too.
Do not define "ViewModel.cs"
". cshtml" Inside
@using (Html.BeginForm ()) {<p>account: @Html. TextBox ("Login.name",string. Empty) </p> <p>Password: @Html. Password ("LOGIN.PSD",string. Empty) </p> <p>Age: @Html. TextBox ("Person.age",string. Empty) </p> <p>Other: @Html. TextBox ("Person.other",string. Empty) </p> <input type="Submit"Value="Login"/>}
Background controller:
Public " Login ")] Loginmodel Objlogin,[bind (prefix="person")]personmodel objperson) { xxx ... return View ();}
We can bind the data directly to "Objlogin" and "Objperson", the data get, want to do what, casually.
MVC Post data binding for multiple models on an action