发布选中单选按钮的行

时间:2010-05-25 12:52:34

标签: c# asp.net-mvc

查看:

<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>

控制器操作:

    [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();
    }

我尝试发布数据行,但是单选按钮被检查但是失败了,有什么问题?

1 个答案:

答案 0 :(得分:0)

您的Numbers方法不应该接受FormCollection而不是int []吗?

这样的东西似乎有用,但不知道你的功能,很难说它是否适合你的目的:

[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();

}