ASP.NET MVC Get submit data

Đoạn mã dưới đây dùng FormCollection để đọc dữ liệu submit từ form lên server (ASP.NET MVC)

View:

@{
var list =new[] { “New York”, “Philadelphia”, “California” };
}

@*@using (Html.BeginForm(“Register”,”Home”,FormMethod.Post))*@
@using (Html.BeginForm())
{

@Html.Label(“User Name:”)<br/>
@Html.TextBox(“UserName”)<br/><br/>
@Html.Label(“Address:”)<br/> @Html.TextArea(“Address”)<br/><br/>
@Html.Label(“Password:”)<br/>@Html.Password(“Password”)<br/><br/>
@Html.Label(“I need updates on my mail:”)
@Html.CheckBox(“ChkEmail”)<br/> <br/>
@Html.Label(“Select your city:”)
@Html.DropDownList(“City”, new SelectList(list), “Choose”) <br/>
@Html.Label(“Male:”)@Html.RadioButton(“Gender”, “Male”, true) <br/>
@Html.Label(“Female:”)@Html.RadioButton(“Gender”, “Female”) <br/> <br/>
<br/><br/>
<input type=”submit” value=”Submit”>
}

Controller:

public ActionResult Register()
{
return View();
}
[HttpPost]
public ActionResult Register(FormCollection fields)
{
string username = fields[“UserName”];
string address = fields[“Address”];
string password = fields[“Password”];
//string email = fields[“ChkEmail”];
bool email = fields[“ChkEmail”] == “false” ? false : true;
//Get value of checkbox

string city = fields[“City”];
string gender = fields[“Gender”];
ViewBag.UserName = username;
ViewBag.Address = address;
ViewBag.Password = password;
ViewBag.Email = email;
ViewBag.City = city;
ViewBag.Gender = gender;
return View(“RegistrationInfo”);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s