Hi Friends,in this article I will explain about MVC4 Razor:How to retrieve view values on POST method in Controller using FormCollection.
In previous articles i already explained about JSON: Create Cascading DropDownList from Database using JQuery in MVC 4 Razor,How to bind DropDownList from database in C# MVC 4 razor and How to Create Cascading DropDownList in MVC 4 Razor and JQuery.
And Run the application,the output will be as shown in the below figure.
In previous articles i already explained about JSON: Create Cascading DropDownList from Database using JQuery in MVC 4 Razor,How to bind DropDownList from database in C# MVC 4 razor and How to Create Cascading DropDownList in MVC 4 Razor and JQuery.
1. FormCollection is use to access Form's post Data on the controller.
2. FormCollection class contains the form value providers for the
application.
Following is the way we use FormCollection :
Take one Model Class Account.cs
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
namespace MvcCRUD.Models
{
public
class Account
{
public string
firstName { get; set;
}
public string
lastname { get; set;
}
public string
address { get; set;
}
}
public int Register() { //Write the code to insert in database }
}
|
And Take one Controller
AccountController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcCRUD.Models;
namespace MvcCRUD.Controllers
{
public
class AccountController
: Controller
{
public ActionResult
Register()
{
return View();
}
}
}
|
Create View as Register.cshtml
@model MvcCRUD.Models.Account
@{
ViewBag.Title = "Register";
}
<h2>Register</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Account</legend>
<div class="editor-label">
@Html.LabelFor(model
=> model.firstName)
</div>
<div class="editor-field">
@Html.EditorFor(model
=> model.firstName)
@Html.ValidationMessageFor(model
=> model.firstName)
</div>
<div class="editor-label">
@Html.LabelFor(model
=> model.lastname)
</div>
<div class="editor-field">
@Html.EditorFor(model
=> model.lastname)
@Html.ValidationMessageFor(model
=> model.lastname)
</div>
<div class="editor-label">
@Html.LabelFor(model
=> model.address)
</div>
<div class="editor-field">
@Html.EditorFor(model
=> model.address)
@Html.ValidationMessageFor(model
=> model.address)
</div>
<p>
<input type="submit"
value="Create"
/>
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back
to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
|
We can use FormCollection in 2 ways.
Way 1:
[HttpPost]
public ActionResult
Register(FormCollection frm)
{
Account obj = new
Account();
obj.firstName =
frm["firstName"].ToString();
obj.lastname =
frm["lastname"].ToString();
obj.address = frm["address"].ToString();
int retval = Convert.ToInt32
(obj.Register());
if (retval == 1)
{
return View("~/Home/index");
}
else
{
return
View("Register");
}
}
|
Way:2
[HttpPost]
public ActionResult
Register(FormCollection frm)
{
Account obj = new
Account();
obj.firstName =
frm[0].ToString();
obj.lastname =
frm[1].ToString();
obj.address =
frm[2].ToString();
int retval = Convert.ToInt32
(obj.Register());
if (retval == 1)
{
return View("~/Home/index");
}
else
{
return
View("Register");
}
}
|
And Run the application,the output will be as shown in the below figure.
"If you like my blog or articles, you can appreciate by leaving your comments or Liking my Facebook pageAspdotnet-kishore, following on Google+ Aspdotnet-Kishore, Twitter on AspdotnetKishore, Linked in Aspdotnet-Kishore, stumbling my posts on stumble upon and subscribing on RSSfeed Aspdotnet-Kishore for free updates directly to your Email inbox . Watch my blog for more articles."
No comments:
Post a Comment