tags:

views:

32

answers:

1

View:

<form id="numbers-form" method="post" action="/Numbers/Numbers">
<table id="numbers">
    <tr>
        <th>
            prvi_br
        </th>
        <th>
            drugi_br
        </th>
        <th>
            treci_br
        </th>
    </tr>
<% int rb = 1; %>
<% foreach (var item in Model) { %>

    <tr>

        <td>
        <%= Html.Encode(item.prvi_br) %>
        <input type="radio" name="<%= Html.Encode(rb) %>"  value="<%= Html.Encode(rb) %>" id='<%= Html.Encode(item.prvi_br) %>'/>
        </td>
        <td>
        <%= Html.Encode(item.drugi_br) %>
        <input type="radio" name="<%= Html.Encode(rb)%>" value="<%= Html.Encode(rb) %>" id='<%= Html.Encode(item.drugi_br) %>'/>
        </td>
        <td>
        <%= Html.Encode(item.treci_br) %>
        <input type="radio" name="<%= Html.Encode(rb)%>" value="<%= Html.Encode(rb) %>" id='<%= Html.Encode(item.treci_br) %>'/>
        </td>
    </tr>
<% rb++; %>

<% } %>
</table>
        <p>
            <input type="submit" value="Save" />
        </p>
</form>

Controller action:

    [HttpPost]
    public ActionResult Numbers(int[] rb)
    {
        brojevi br = new brojevi();
        for (int i = 1; i <= rb.Length; i++) //in this line I have error:Object reference not set to an instance of an object.
        {
            br.prvi_br = i;
            br.drugi_br = i+1;
            br.treci_br = i+3;
        }
        numbers.AddTobrojevi(br);
        numbers.SaveChanges();
        return View();
    }

I try to post data row in wich radio button is checked but failed, what is wrong??

A: 

Shouldn't your Numbers method accept a FormCollection instead of an int[]?

Something like this seems to work, but not knowing your functionality, it's hard to tell if it will suit your purpose:

[HttpPost]
public ActionResult Numbers(FormCollection fc)
{

    foreach (string key in fc.Keys)
    {
        int i = Convert.ToInt32(key);
        // i = the number of the item that was selected.
    }

    return View();

}
Robaticus
all radio buttons in one row have same name, that is problem because i= same number whatever radio in this row is selected
Ognjen
Then change the name of your radio button in your code to something more unique?
Robaticus
If it works with the same name then it is possible to check all three radio buttons simultaneously and it would not be allowed
Ognjen
Well, your other option is to actually use model binding to create the radio buttons. See Html.RadioButtonFor() method.If you do that, you can just update your model from the formCollection on execution.
Robaticus
Any example for that??
Ognjen