ASP .NET MVC:Html.Radiobutton onclick -- 设置 ViewData[“”]

发布于 2024-07-24 07:07:54 字数 302 浏览 2 评论 0原文

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

我想知道是否有一种方法可以在选择单选按钮时但在提交页面之前设置 ViewData 变量。

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

我想一定有办法做一个 onclick="some C# function that set ViewData(args)"

谢谢

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

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

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.

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

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浊酒尽余欢 2024-07-31 07:07:54

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

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

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

如果您采用第一种方法,对于那些未启用 Javascript 的人来说,很容易转向第二种方法。

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

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

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

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

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.

楠木可依 2024-07-31 07:07:54

ViewData 仅在请求的生命周期内存在,并且仅存在于服务器端。 因此,一旦页面呈现,该对象就不再存在。

您可以采取的一些替代方法:

1 - 使用客户端 Javascript 根据需要添加表单和输入。 更多信息请点击这里:
ASP.NET MVC 和 JQuery 动态表单内容

2 - 预渲染新表单,但通过 CSS 隐藏它,并在单击相应的单选按钮时取消隐藏。 更多信息请点击这里:
展开折叠 html 字段 Firefox

3 - 在适当的单选时使用 AJAX 渲染新表单单击按钮。 更多信息请点击这里:
http://www.asp.net/learn/mvc/tutorial- 32-cs.aspx

ViewData only exists, and only exists server-side, for the lifetime of the request. So, once the page is rendered the object no longer exists.

Some alternate approaches you can take:

1 - Use client-side Javascript to add a form and inputs as necessary. More info here:
ASP.NET MVC & JQuery Dynamic Form Content

2 - Pre-render the new form, but hide it via CSS, and unhide it when the appropriate radio button is clicked. More info here:
expand collapse html field Firefox

3 - Use AJAX to render the new form when the appropriate radio button is clicked. More info here:
http://www.asp.net/learn/mvc/tutorial-32-cs.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文