0

I have question about accessing html.checkbox() in controller method.

In my view i have

<% foreach (var item in Model.PredmetTbl){  %>
  <td>
     <%:Html.CheckBox(item.Predmet) %>
     <%:item.Predmet %>
  </td>
<%} %>

Predemts are in DB and I want create new db records. How can i test if the checbox is checked or not ?

My controller code

[HttpPost]
    public ActionResult PridajSaduPredmetov(int id, FormCollection data)
    {
        var zoznam = from predmet in ziakDB.PredmetTables select predmet;

        ZoznamPredmetovTable predmety;

        foreach (var item in zoznam)
        {

            if (HERE TESTING IF CHECKED)//IF Checked==true will add to db
            {
                predmety = new ZoznamPredmetovTable();

                predmety.ZiakID = id;
                predmety.PredmetID = item.PredmetID;
                predmety.SkolskyRokID = IndexViewModel.GetSkolskyRokTeraz();
                try
                {
                    ziakDB.ZoznamPredmetovTables.InsertOnSubmit(predmety);
                    ziakDB.SubmitChanges();
                }
                catch { }
            }
        }
        return RedirectToAction("DetailZiaka", "Administration", new { id = id });
    }
OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
Peter M.
  • 1,028
  • 2
  • 10
  • 27

2 Answers2

0

(controlid).checked will return true/false

MRR0GERS
  • 652
  • 5
  • 18
0

I did not make use of the html checkbox extension but I based my solution on this post: How to handle checkboxes in ASP.NET MVC forms?

<% For Each item As x In Model.predmetTbl%>
                    <div><input type="checkbox" name="SelectedPredMet" 
                        <% If Model.SelectedPredMet.Contains(item.Id) Then%>
                            checked="checked"
                        <% End If %>
                        value="<%: item.Id %>" /> <%: item.Predmet %></div> 
                <% Next%>
Community
  • 1
  • 1
David
  • 4,185
  • 2
  • 27
  • 46
  • on the page you provided i found solution :) bool isChecked = data.GetValues(item.Predmet).Contains("true"); if (isChecked){........} – Peter M. Jun 29 '11 at 20:40