ASP .NET MVC: Html.Radiobutton onclick -- 设置 ViewData[""] [英] ASP .NET MVC: Html.Radiobutton onclick -- set ViewData[""]

查看:35
本文介绍了ASP .NET MVC: Html.Radiobutton onclick -- 设置 ViewData[""]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ASP .NET MVC 和 Web 编程的新手.

I'm new to ASP .NET MVC and to web programming in general.

我想知道是否有办法在选择单选按钮时设置 ViewData 变量 - 但在提交页面之前.

I'm wondering if there is a way to set ViewData variables when a radiobutton is selected -- but before the page is submitted.

也许我找错了树,但我想要做的是创建一个表单,根据选择的单选按钮添加新字段.所以我想要做的是,当单击单选按钮时,它会设置一个 ViewData 变量,并基于该 ViewData 变量,不同的部分视图加载当前字段下方的相应字段.

Maybe I'm barking up the wrong tree but what I'm trying to do is create a form where new fields are added based on which radio button is selected. So what I want to do is when a radiobutton is clicked it sets a ViewData variable and based on that ViewData variable a different partial view loads the appropriate fields below the current field.

我想一定有办法做一个 onclick="some C# 函数来设置 ViewData(args)"

I imagine there must be someway of doing a onclick="some C# function that sets ViewData(args)"

谢谢

推荐答案

有几种方法可以解决这个问题.

There are a couple of ways you could go about this.

1) 您可以有一个 Ajax 表单,通过 Javascript 将表单发回并检查它是否是 Ajax 请求,然后将部分视图返回到您指定的 div.

1) You could have an Ajax form where through Javascript you post the form back and check to see if it's an Ajax Request, there by returning a partial view to a div that you specify.

2) 按原样发布表单并检查服务器端以查看是否单击了单选按钮,从而重新显示表单并显示新选项.

2) Post the form as is and check server-side to see if the radio button was clicked, and thus redisplay the form with the new options visible.

如果您采用第一种方法,对于那些没有启用 Javascript 的人来说,很容易进入第二种方法.

If you take the first approach it would be easy enough to fall through to the second one for those without Javascript enabled.

没有真正的onclick"事件,因为我假设您已经习惯了 Webforms,您基本上必须使用自己的 Javascript 来处理这些事情.一旦你做了一些,我认为你会发现它真的不是太糟糕,好处是你可以更好地控制你正在做的事情,并通过它更好地了解更大的图景.

There aren't really "onclick" events as I'm assuming you are used to from Webforms, you would basically have to roll your own Javascript to handle such things. Once you do a few, I think you'll find it's really not too bad, with the benefit that you'll have more control over what you're doing and through that gain a better understanding of the larger picture.

这篇关于ASP .NET MVC: Html.Radiobutton onclick -- 设置 ViewData[""]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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