[RESOLVED]Binding Radio Button in MVC giving Null Exception

Hi,

I am trying to bind Radio Button in MVC. I am new to MVC. Please help me.

In Model class:-

    public class Alert
    {
        public int Id { get; set; }

        public string GetAlert { get; set; }
    }
    public class Member
    {
        public List<Alert> alert { get; set; }
    }

In DataAccess class:-

public List<Alert> RecAlert()
        {
            Alert alert = new Alert();
            List<Alert> list = new List<Alert>();
            con.Open();
            SqlCommand cmd = new SqlCommand("select Id,Alert from tblAlert", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);

            for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
            {
                //Alert al = new Alert();
                alert.Id = Convert.ToInt32(ds.Tables[0].Rows[i]["Id"]);
                alert.GetAlert = ds.Tables[0].Rows[i]["Alert"].ToString();
                list.Add(alert);
            }

            con.Close();
            return list;
        }

In Controller class

        public ActionResult RecAlerts()
        {
            DAO.DataAccess da = new DAO.DataAccess();
            List<Alert> alert = new List<Alert>();
            alert = da.RecAlert();
            return View(alert);
        }

Inside view

@model  MvcApplicationEmpty.Models.Member

@{
    ViewBag.Title = "AddMember";
}

<h2>AddMember</h2>

<table>
    <tr>
        <td style="width: 33%">
            Receive Alerts
        </td>
        <td>
             @Html.RadioButtonFor(m=>m.alert,Model.alert)
        </td>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
            <input type="submit" name="btnSubmit" value="Register" />
        </td>
    </tr>
</table>

It is giving below error.

Value cannot be null.
Parameter name: value

Please help
Rahul

Rahul_agg11

@Html.RadioButtonFor(m=>m.alert,Model.alert)

try this

@{
    foreach (var alert in Model.alert)
    { 
        @Html.RadioButton("alert", alert.Id)@alert.GetAlert
    }
}

Hi,

Thanks for replying.

 @{
                foreach (var alert in Model.alert)
                { 
                    @Html.RadioButton("alert", alert.Id)@alert.GetAlert
                }
            }

Its still giving error on foreach statement.

Null Refernce exception was unhandled by user code.

Object reference not set to an instance of an object.

Thanks,

Rahul

Have you tried setting your alert property on a Member object like the one that you are currently using and passing that into your View :

public ActionResult RecAlerts()
{
         DAO.DataAccess da = new DAO.DataAccess();
            
         // Build your model (and populate your List)
         Member model = new Member(){ alert = da.RecAlert() };

         // Pass your model to your View
         return View(model);
}

Additionally, you’ll want to place a breakpoint prior to your return View(model); line above to ensure that your alert property was properly set. If that is the case, then you should be able to use the code you provided above (although to be safe, you
might want to add some null checking in there as well
) :

@if (Model != null && Model.alert != null)
    {
        foreach (var alert in Model.alert)
        {
            @Html.RadioButton("alert", alert.Id) @alert.GetAlert
        }
    }
 

Rahul_agg11

@model MvcApplicationEmpty.Models.Member

since the view expecting the model of type Member.

Rahul_agg11

return View(alert);

But in above statement you are returning List<Alert> to render the view. how come the gets render

The action must return the object(s) of type whatever View is expecting. 

So the action should return something like this

 public ActionResult RecAlerts()
        {
            
            List<Alert> alerts = new List<Alert>();
            for (int i = 1; i <= 10; i++)//get from database
            {
                Alert alert = new Alert();
                alert.Id = i;
                alert.GetAlert = "Alert "+i.ToString();
                alerts.Add(alert);
            }
            Member member = new Member();
            member.alert = alerts;
            return View(member);
        }

You might be able to clean up your existing method that populates your records as well :

public List<Alert> RecAlert()
{
        // Build your list of alerts to return    
        List<Alert> list = new List<Alert>();
       
        // You should consider using a using statement to build your connection here (to avoid memory leaks)
        using(var con = new SqlConnection("Your Connection String"))
        {
               // Open your connection
               con.Open();

               // Build your command (and query)
               using(var cmd = new SqlCommand("SELECT Id, Alert FROM tblAlert", con)) 
               {
                      // Execute your queyry and read through your results
                      using(var reader = cmd.ExecuteReader())
                      {
                             // Read through your rows and build your alerts
                             while(reader.Read())
                             {
                                   // Build an alert
                                   var alert = new Alert();
                                   alert.Id = Convert.ToInt32(reader["Id"]);
                                   alert.GetAlert = reader["Alert"];

                                   // Add this to your list
                                   list.Add(alert);
                             }    
                      }
               }
        }

        // Return your list
        return list;
}

Leave a Reply