为什么当我点击某个行的按钮时,却继续传递第一行的值 [英] Why when i click button for a certain row, but keeps on passing value of first row

查看:91
本文介绍了为什么当我点击某个行的按钮时,却继续传递第一行的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将选定行的值传递给控制器​​操作,以便将值作为会话传递。价值传递给控制器​​正在工作,但事情是,当我按下某个行的按钮时,它继续传递第一行的值。

 < form Class =form-horizo​​ntalrole我怎样才能让每一行的按钮分别传递行的值? =formaction =@ Url.Action(Lookup,home)method =post> 
@For Model.Details中的每个项目
@< tr Class =even gradeC>
< td>< input type =hiddenid =idname =idvalue =@ item.id> @ item.id< / td>
< td>< input type =hiddenid =namename =namevalue =@ item.name> @ item.name< / td>
< td style =text-align:center; vertical-align:middle;>< Button type =submitClass =btn btn-defaultname =btnSelectvalue =@ item.id>选择< /按钮>< / TD>
< / tr>
下一个
< / form>

这是在控制器中:

<$ p (btnSelect As String,id As String,name As String)As ActionResult
如果btnSelect.Equals(id)则
Session(id)= id
Session(name)= name
End If
End Function

| 1 | Ana |选择|



| 2 |贝拉|选择|



当我按第2行的按钮时,它传递值'1'和'Ana'。我已经尝试将id的值赋给每个按钮来标识按钮,但它似乎仍然通过第一行的值,所有时间。



当我在断点处查看值时,按钮的值是正确的 - '2',但'id'和'name'的值始终为'1'和'Ana'。我在这里错过了什么?任何人都可以帮助我吗?

解决方案

您的< form> 元素,它会在每次迭代中为您的隐藏输入生成重复的名称属性(您有多个输入,其中 name =id code>和 name =name)。



当您点击一个提交按钮时,发布所有输入的名称/值对,并且 DefaultModelBinder 仅读取第一个具有匹配名称的名称并忽略其余部分(除非您的参数为 IEnumerable (Of String)在这种情况下,参数将包含所有值的集合)。

您的代码也会生成无效的html,因为重复 id 属性



你可以通过生成一个新的< form> / code>在每次迭代中

  @For Model.Details中的每个项目
@< tr Class =偶数级C>
< td> @ item.id< / td>
< td> @ item.id< / td>
< td>
< input type =hiddenname =idvalue =@ item.id/>
< input type =hiddenname =namevalue =@ item.name/>
< button type =submitclass =btn btn-default>选择< / button>
< / form>
< / td>
< / tr>
下一个

请注意 id 属性已被删除,并且该按钮中的名称属性已被删除。因为它的值是 @ item.id 而你的如果btnSelect.Equals(id)Then 代码行总是返回 True 使其毫无意义。你的控制器方法应该只是

  Function Lookup(id As String,name As String)As ActionResult 
Session( id)= id
Session(name)= name
....
End Function

注意:您并不需要隐藏的输入,您可以简单地将值添加为路径或查询sting值,格式为 action 属性

  action =@ Url.Action(Lookup,home,New With {.id = item .id,.name = item.name})


I would like to pass value from a chosen row to the controller action so that the value will be passed as a session. The passing of value to controller is working, but the thing is, when i press a certain row's button, it keeps on passing the value of the first row. How can i make so that each row's button passes the row's values respectively?

<form Class="form-horizontal" role="form" action="@Url.Action("Lookup", "home")" method="post">
    @For Each item In Model.Details
        @<tr Class="even gradeC">
            <td><input type="hidden" id="id" name="id" value="@item.id">@item.id</td>
            <td><input type="hidden" id="name" name="name" value="@item.name">@item.name</td>
            <td style="text-align: center; vertical-align: middle;"><Button type="submit" Class="btn btn-default" name="btnSelect" value="@item.id">Select</Button></td>
        </tr>
     Next
 </form>

This is in the controller:

Function Lookup(btnSelect As String, id As String, name As String) As ActionResult
  If btnSelect.Equals(id) Then
     Session("id") = id
     Session("name") = name
  End If
End Function

| 1 | Ana | Select |

| 2 | Bella | Select |

When i press button at row 2, it passes value '1' and 'Ana'. I already tried putting the value of id to each button to identify the buttons, but it seemed like it still passes the values of first row, all the times.

When i looked at the values during breakpoint, the value of button is correct - '2' but the value for 'id' and 'name' is always '1' and 'Ana'. What am i missing here? Could anyone help me please?

解决方案

You have a loop inside your <form> element which is generating duplicate name attributes for your hidden inputs in each iteration (you have multiple inputs with name="id" and name="name").

When you click a submit button, all the name/value pairs of all inputs are posted and the DefaultModelBinder only reads the first one with a matching name and ignores the rest (unless you parameters were IEnumerable(Of String) in which case the parameters would contain an collection of all values).

You code is also generating invalid html because of duplicate id attributes.

You can solve this by generating a new <form> in each iteration

@For Each item In Model.Details
    @<tr Class="even gradeC">
        <td>@item.id</td>
        <td>@item.id</td>
        <td>
            <form Class="form-horizontal" role="form" action="@Url.Action("Lookup", "home")" method="post">
                <input type="hidden" name="id" value="@item.id" />
                <input type="hidden" name="name" value="@item.name" />
                <button type="submit" class="btn btn-default">Select</button>
            </form>
        </td>
    </tr>
Next

Note that the id attributes have been removed, and the name and value attribute from the button has been removed. There is no point in passing back a value for the button since its value is @item.id and your If btnSelect.Equals(id) Then line of code always returns True making it pointless. You controller method should be just

Function Lookup(id As String, name As String) As ActionResult
    Session("id") = id
    Session("name") = name
    ....
End Function

Side note: You do not really need the hidden inputs, and you can simply add the values as route or query sting values in the forms action attribute

action="@Url.Action("Lookup", "home", New With { .id = item.id, .name = item.name })"

这篇关于为什么当我点击某个行的按钮时,却继续传递第一行的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆