我如何判断单选按钮是否已选中? (MVC)
我真的很希望能够做到这一点,
if myRB.Checked = true then
return redirecttorout("SomeRoute")
else
someother route
我习惯了网络表单,我可以在后面的代码中做到这一点
I would really like to be able to do this
if myRB.Checked = true then
return redirecttorout("SomeRoute")
else
someother route
i am used to web forms, in which i could do this in the code behind
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Mvc 与普通 html post 一样,通过输入的名称而不是 id 返回值。在单选按钮集合中,所有单选按钮都具有相同的名称。所以你得到的是单选按钮的名称和所选单选按钮的值
代码最终看起来像
Mvc like normal html post returns values by an input's name not id. In a radio button collection all radio buttons have the same name. So what you get back is the name of the radio button with the value of the selected radio button
The code ends up looking like
在表单发布到的操作中,您需要创建一个字符串 var 来保存该值。
单选按钮的名称都需要在视图上匹配,但值将是发送到操作的值。确保上面看到的字符串与单选按钮的名称匹配。检查字符串是否为 null 或为空也是一个好主意。
In the action that the form posts to you need to create a string var that will hold the value.
The Name of the radio buttons all need to match on the view, but the value will be what is sent to the Action. Ensure that the string as seen above matches the Name of the radio buttons. It would also be a good idea to check that string for is null or empty.
查看
型号
控制器
View
MODEL
CONTROLLER