简体   繁体   English

选中单选按钮的帖子行

[英]post row where radio button is checked

I try to post data row in which radio button is checked but failed, what is wrong?我尝试发布选中单选按钮但失败的数据行,有什么问题?

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

Shouldn't your Numbers method accept a FormCollection instead of an int[]?您的 Numbers 方法不应该接受 FormCollection 而不是 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();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM