Monday, 21 August 2017

Bind (Populate) RadioButtonList From DataBase Table Using EntityFrameWork In Asp.Net MVC

8/21/2017 - By Pranav Singh 0

In this article I will show you how you can bind assign data from database table to radiobuttonlist using entity framework in asp.net mvc using c#.net.

Some of my previous articles are as follows:


So for this article first we will create a new asp.net application and add and entity file in it.



After adding entity file we will add a model class file and add the below code.

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

namespace MVC_Demos.Models
{
    public class RadioButtonLiatModel
    {
        public List<StudentRadioButtonData> RadioButtonListData { get; set; }
    }
    public class StudentRadioButtonData
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

After this we will add a controller class file and add the below code.

public ActionResult Index()
        {
            RadioButtonLiatModel _radiobuttonliatmodel = new RadioButtonLiatModel();
            _radiobuttonliatmodel.RadioButtonListData = new List<StudentRadioButtonData>();

            //Retrive data from entity table
            DemoEntities _demoentities = new DemoEntities();
            var data = _demoentities.StudentDetails.ToList();
            //Bind data to model class
            foreach (var item in data)
            {
                _radiobuttonliatmodel.RadioButtonListData.Add(new StudentRadioButtonData { Id = item.Id, Name = item.Name });
            }
            return View(_radiobuttonliatmodel);
        }

Here in above code I have created object for entity file and the retrieved the value from database table entity  object. After this we will create a view and add the below code into it.

@model MVC_Demos.Models.RadioButtonLiatModel
@{
    ViewBag.Title = "Bind RadioButtonList From DataBase Table Using EntityFrameWork In Asp.Net MVC";
}
<h2>RadioButton List</h2>
<br />

@foreach (var item in Model.RadioButtonListData)
{
 <input type="radio" id="@item.Id" value="@item.Name" name="radiobutton" />@item.Name
}


Hear in above code I have applied look to generate the radio button list items.  Now we have done lets check the output. First we will check the values at code end that what we are getting from entity table.


Now we have done run the application to check the output.


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top