tags:

views:

104

answers:

2

i updated code and this is the corect solution of problem

    [AcceptVerbs(HttpVerbs.Post)]           
    public ActionResult Edit(int[] rb, int id)
    {
        List<nastava_prisustvo> nastava = new List<nastava_prisustvo>();

        string poruka = "";
        for (int i = 1; i <=rb.Length; i++)
        {
            string name = "chk" + i;
            string selID = Request.Form[name];  

            if (selID == "on")
            {
               //poruka = poruka + "Polje sa rednim brojem "+ i +" je chekirano\n";

                nastava.Add(new nastava_prisustvo
               {
                br_indexa = int.Parse(Request.Form["id_stud"+i]),
                id_predmet = id
               });
            }
        }
       // ViewData["Message"] = poruka;
        return View("show", nastava);
    }

View:

 <table class="data-table">        
    <tr> 
         <th>
            Redni br.
        </th>
        <th>
            Br. Indexa
        </th>
        <th>
            Prezime
        </th>
        <th>
            Ime
        </th>
        <th>
           <input id="check_all" type="checkbox" onclick="function" />
        </th>  
    </tr>

<% int rb = 1;%>
<% foreach (var item in Model)
   { %>

    <tr>
    <td>
            <input readonly="readonly" class="input-box" id="rb" type="text" name="rb"  value="<%= Html.Encode(rb)%>" />
        </td>

        <td>

            <input readonly="readonly" class="input-box" id="id_stud" type="text" name="id_stud"  value="<%= Html.Encode(item.id_stud)%>" />

        </td>
        <td>
            <%= Html.Encode(item.prezime)%>
        </td>
        <td>
             <%= Html.Encode(item.ime)%>
        </td>
         <td>
           <input id="check"  name="chk<%= Html.Encode(rb)%>" type="checkbox"  /> 
        </td>
    </tr>

<% rb = rb + 1;%>
<% } %>

</table>
A: 

Here is an example demonstrates how to pass checkbox's value to controller.

View:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%= Html.Encode(ViewData["Message"]) %></h2>
    <p>
        To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc&lt;/a&gt;.
        <form action="/home/index" method="post">
        <input type="checkbox" name="cb" value="value1" />
        <input type="checkbox" name="cb" value="value2" />
        <input type="submit" />
        </form>
    </p>
</asp:Content>

Controller:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(string cb)
    {
        ViewData["Message"] = cb;

        return View();
    }

This example demonstrates how to receive via array.

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(string[] cb)
    {
        string result = "";
        foreach (string value in cb)
            result += value.ToString() + " ";
        ViewData["Message"] = result;

        return View();
    }


   <form action="/home/index" method="post">
    <input type="checkbox" name="cb" value="v1" />
    <input type="checkbox" name="cb" value="v2" />
    <input type="checkbox" name="cb" value="v3" />
    <input type="checkbox" name="cb" value="v4" />
    <input type="submit" />
    </form>
Raymond
In page show I want to see only checked checkbox
Ognjen
Seems like that would work ok for a radio, but a checkbox can get multiple values posted back. You might need to receive it as an array of the expected type.
tvanfosson
By default, only checkbox in "On" state will be posted to server.
Raymond
I updated code but problem is stay
Ognjen
When write if (check[j]=="on") I have this error:Index was outside the bounds of the array.
Ognjen
If these checkbox share the same name, only one of these value can be posted. If they are in the format of name[0], name[1], etc to construct an array parameter, the default modal binder may return null for this parameter if posted items' name are non-continuous.So, it may require some tricks.
Raymond
You got this error because there're no more than j items are posted to server. But we can't tell the reason from your server side code. It depends on how you wrote the html page and which items did you check.
Raymond
html code for checkbox in my aspx page:<input name="check" type="checkbox" />
Ognjen
This way, your check parameter can only have one element or be null.
Raymond
I updated the example, but note as I stated previously, you must select all checkboxs to run the example.
Raymond
how to dynamically add a number in parentheses
Ognjen
<% for (int i = 0; i < 6; ++i) { %> <input type="checkbox" name="Items[<%= i %>]" /> <% } %>
Raymond
I created the name in your instructions but still have the old problem in line: if (check[j] =="On")error:Object reference not set to an instance of an object.
Ognjen
The example requires you select all items to work. Refer to my previous explanations.
Raymond
I tried but the problem remained
Ognjen
updated according to oo's answer. try again.
Raymond
I updated code and everything works as you said. Just need a trick of which you speak
Ognjen
no need of trick anymore.
Raymond
Why then this code does not work when I not select all checkbox
Ognjen
Because the default ModalBinder can't handle the situation as you expect.
Raymond
Does this mean that it should change default ModalBinder
Ognjen
not necessarily, because we can get over it with code in most recent example. it's a problem exists in our old sample code.
Raymond
I do not understand how to solve this problem
Ognjen
what's the problem with your code now ?
Raymond
A: 

if you have a list of checkboxes and you want to pass the checked ones to the controller, all you need to do is the following:

name of each check box in the view "items"

controller Action:

 public ActionResult(UpdateObject updateobject)
 {
      string[] list = updateobject.items;
 }

Update object definition:

 public class UpdateObject
 {
      public string[] items { get; set; }
 }
ooo
I do not understand what should I do with this code
Ognjen
i was just showing you that you can create a bunch of checkboxes in a view and give them the same name and when you post your form, you can have an array of the checked ones passed into your controller action
ooo