10 May 2017

Get Text of selected Radio Button in ASP.NET MVC

Get Text of selected Radio Button in ASP.NET MVC

File => New Project
Templates => Visual C# => ASP.NET MVC 4 Web Application
Controllers => Add Controller=> Add

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/

public ActionResult Index()
{
return View();
}
public string CheckRadio(FormCollection fm)
{
string genRad = fm["Gender"].ToString();
return "Selected gender is " + genRad;
}

}
}

Add View => Add

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>
@using(Html.BeginForm("CheckRadio", "Home", FormMethod.Post ))
{
<div>

Select Gender:<br />
Male @Html.RadioButton("Gender", "Male")
Female @Html.RadioButton("Gender", "Female")

@* or you can use below syntax for radio buttons
Male<input type="radio" name="gender" value="Male"/>
Female<input type="radio" name="gender" value="Female" />*@
</div>
<input type="submit" value="Submit"/>
}


1 comment: