How to get controls values like textboxes, checkbox, radiobuttons and dropdownlist back to controller from Razor view in mvc3


Code in Razor view:
@{
    ViewBag.Title = "Home Page";
}

<h2>@ViewBag.Message</h2>
<img src="@Url.Content("~/Content/picture1.jpg")" />

@using(@Html.BeginForm("Result","Home",FormMethod.Get))
{
<div>
Enter UserName:
@Html.TextBox("stname")
<br />
<br />
Specifie the Timings:
@Html.RadioButton("Timinings","Morning",true) Morning
@Html.RadioButton("Timinings","Evening",false)Evening
@Html.RadioButton("Timinings","Anytime",false) AnyTime
<br />
<br />
Courses List;
@Html.DropDownList("Courses")
<br />
Receive Mails:
@Html.CheckBox("chk")
<input type="submit" id="btnsubmit" value="Submit" />
</div>
}
Code in Controller:
public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to the Training Courses...";
            List<string> objcourses = new List<string>();
            objcourses.Add("Asp.Net");
            objcourses.Add("MVC");
            objcourses.Add("WCF");
            objcourses.Add("WPF");
            objcourses.Add("C#.Net");
            ViewBag.Courses = new SelectList(objcourses);
            return View();
        }

        public ActionResult Result(string stname, string Timinings, string Courses, bool chk)
        {
            ViewBag.stname = stname;
            ViewBag.Timinings = Timinings;
            ViewBag.Courses = Courses;
            if(chk == true)
            ViewBag.chk = "EmailSubscribed";
            else
                ViewBag.chk = "EmailNotSubscribed";
            return View();

        }
}
Advertisements

2 thoughts on “How to get controls values like textboxes, checkbox, radiobuttons and dropdownlist back to controller from Razor view in mvc3

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