MVC Post data binding for multiple models on an action

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.