How To insert Radio Button Data in DataBase -- MVC

View code :

  <div class="form-horizontal">
        <h4>MyRadioButtonDemo</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group ">
            @Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10 " >
                <label class="radio-inline">
                    @Html.RadioButtonFor(model=>model.Gender,"Male",new { @name="gender"})
                    male
                </label>
                <label class="radio-inline">
                    @Html.RadioButtonFor(model => model.Gender, "FeMale",new { @name="gender"})
                    Female
                </label>
                <label class="radio-inline">
                    @Html.RadioButtonFor(model=>model.Gender,"Other",new { @name="gender"})
                    Other
                </label>
               
            
            </div>
        </div>


Model Code :

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

namespace Test.RadioButtonModel
{
    public class MyRadioButtonDemo
    {
        public string id { get; set; }
        public string Gender { get; set; }
    }
    
}

Controller Code :


  [Route("Radio")]
        public ActionResult Radio()
        {
            return View();
        }
        [Route("Radio")]
        [HttpPost]
        public ActionResult Radio(MyRadioButtonDemo myRadioButtonDemo)
        {
            FormDbEntities db = new FormDbEntities();
            RadioButtonTABLE tb = new RadioButtonTABLE();
            tb.Gender = myRadioButtonDemo.Gender;
            db.RadioButtonTABLEs.Add(tb);
            int success =  db.SaveChanges();
            if(success>0)
            {
                ViewData["Success"] = "Yes Gender insert in data Base";
            }
            else
            {
                ViewData["Success"] = "Some problem occers";
            }
            
            return View();
        }

Comments

Popular posts from this blog

Search Record From Table Using jQuery in Asp.net core MVC

Calendar in Asp.net

CheckBox in Asp.Net MVC (Insert Data of CheckBox in Database and GetData Of checkBox From DataBase)