Thursday, 17 October 2013

MVC4 Razor:How to retrieve view values on POST method in Controller using FormCollection || MVC 4 Razor:How to Create Simple Register Page/Form in ASP.NET using MVC 4

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.



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

© 2012-2014 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.