Ways to accept views passing data (forms) in MVC

Source: Internet
Author: User

1. Read form data by Request.Form 2, read form data by FormCollection 3, read form data by object

First define a Usermodel class:

  Public classUsermodel { Public intUserID {Get;Set; }//User Number         Public stringUserName {Get;Set; }//User name         Public stringPassword {Get;Set; }//Password}
View Code

The view code is as follows:

@model mvcdemo.models.usermodel@{Layout=NULL;}<! DOCTYPE html>@using (@Html. BeginForm ()) {<div>user name: @Html. textboxfor (Model= Model. UserName,New{@style ="width:200px" })        </div> <div>Password: @Html. passwordfor (Model=model. Password)</div> <div> <input type="Submit"Value="Submit"/></div>    }</body>View Code

How the controller accepts data:

1.request.form

 Public actionresult Useredit ()       {           new  Usermodel ();            = request.form["UserName"];            = request.form["Password"];            return View (model);       }
View Code

2.FormCollection

 Public actionresult useredit (formcollection form)       {           new  Usermodel ();            = form["UserName"];            =form["Password"];            return View (model);       }
View Code

3. Object Read

   Public actionresult Useredit (Usermodel usermodel)        {            Response.Write (usermodel.username);            Response.Write ("<br/>");            Response.Write (Usermodel.password);             return View ();        }
View Code

Ways to accept views passing data (forms) in MVC

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.