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:
How
To Check Radio Button List Item Selected Or Checked In Asp.Net Mvc Using
JavaScript , Get
(fetch) RadioButtonList Checked/Selected Value In Asp.Net MVC Using JavaScript,
Validate
Or Select Radio Button List In Asp.Net MVC Using JavaScript,
Binding
Or Populating Radio Button List in Asp.Net MVC Using Model.
First we will create an sql table and add some value in it.
First we will create an sql table and add some value 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.
0 comments:
Please let me know your view