Adding Radio Button Control in Web API

Introduction

In this article I will show you how to add a RadioButton control in the Web API. We use the radio button in a group in which we select only one at a time. When the user uses the radio button then the user makes a choice among a set of absolute and related options. The user can select only one option from the radio buttons set. Radio Buttons are so called because they work like channels on a radio.

Now see an example of adding the radio button control in the Web API.

Step 1

Create a Web API application as in the following:

  • Start Visual Studio 2012.
  • From the start window select "Installed" -> "Visual C#" -> "Web".
  • Select "ASP.NET MVC4 Web Application" and click the "OK" button.

r.jpg

  • From the "MVC4 project" window select "Web API".

r1.jpg

  • Click the "OK" button.

Step 2

Select the "HomeController.cs" file and write some code. This file exists in:

  • In the "SolutionExplorer".
  • Select "Controller" -> "HomeController".

r2.jpg

Add the following code in this file:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

 

namespace RadioAPI.Controllers

{

    public class HomeController : Controller

    {

        public ActionResult Index()

        {

            return View();

        }

        [AcceptVerbs(HttpVerbs.Post)]

        public string Display(FormCollection form)

        {

            string selectlanguage = form["language"];

            return "Selected Language: <b>" + selectlanguage + "</b>";

 }

     

    }

}

 

Step 3

Add a View page "MVC 4  View Page (ASPX)" named "Index.aspx".

  • In the "Solution Explorer".

  • Right Click on the "Home" -> "Add" -> "New Item".

r3.jpg

  • Select "Installed" -> "Visual C#" -> "Web" -> "MVC 4 View Page(ASPX)".

r4.jpg

  • Click the "Add" button.

Add the following code in this view page:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

 <html>

<head id="Head1" runat="server">

    <title>Index</title>

</head>

<body>

<% using (Html.BeginForm("Display","Home",FormMethod.Post)) { %>

    <div><b>Choose any Language: </b><br />

       <br /> <%= Html.RadioButton("language", "C#") %> C#

        <br /><%= Html.RadioButton("language", "C++") %> C++

        <br /><%= Html.RadioButton("language", "C") %> C

      <br />  <%= Html.RadioButton("language", "Java") %> Java

         <br />  <%= Html.RadioButton("language", "HTML") %> HTML

         <br />  <%= Html.RadioButton("language", "Javascript") %> Javascript

        <br /><br />

        <input type="submit" value="Submit" />

    </div>

    <%} %>

</body>

</html>

Step 4

Execute the application by pressing "F5". The output will be shown like this:

r5.jpg

Select any language and click on the submit button.

r7.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all