Tuesday, 10 June 2014

How to bind radio button with model data ASP.Net MVC




Description:-

In this example we explain that Create Radio button List from the Model in MVC or how to create or bind Radiobutton in mvc or asp.net with mvc razor view.

This is very useful when create a quiz page or online exam aplication where we are going to show some questions with it’s answers to the user and user can select an answer from the available answer options, represented by radio buttons.

To Show Example of How to Upload File in MVC Application then click here upload Image and bind to Gridview in MVC



How to upload File and Fetch file from SqlServer and bind to Gridview fetch file from Sqlserver and bind to Gridview

Let’s suppose that we have a Model for the tbl_hobby and we want to render a radio button list for all hobby. Then Take the following model as an example.

public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
           
            _dbcontext = new HobbyEntities();
           // var list = new _dbcontext.tbl_Employee>
            var model = _dbcontext.tbl_hobby;
            return View(model.ToList<tbl_hobby>());
           
        }


How to start:

 public partial class tbl_hobby
      {
          public int hobby_id { get; set; }
          public string hobby_name { get; set; }
      }


Index.cshtml:-

@model List<MvcApplication1.tbl_hobby>
@{
    ViewBag.Title = "Home Page";
}
@section featured {
    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1>@ViewBag.Title.</h1>
                <h2>@ViewBag.Message</h2>
            </hgroup>
        </div>
    </section>
}
<div>
</div>
<table>
    <tr><td>
            <div id="radioList">
                @for (int i = 0; i < Model.Count; i++)
                {     
                    @Html.LabelFor(a => a[i].hobby_name, Model[i].hobby_name)
                    @Html.RadioButtonFor(a => a[i].hobby_name,
                                Model[i].hobby_id)
                }
            </div>
        </td></tr>
    <tr> <td>
            <input type="button" value="Save All" id="btnsave" />
     </td></tr>
</table>


This entry was posted in :

0 comments:

Post a Comment