我可以在 asp.net mvc 的控制器操作中访问母版页下拉值吗
我有一个母版页,在上面显示用户可以访问的组,现在我想在许多控制器中获取其选定的值以与记录一起保存。我想知道在 asp.net mvc 2 中是否可行,如果不可行,那么有什么解决方法
I have a masterpage on which i displays groups that user can access now i want to gets its selected value in many controllers for saving with the records. I want to know if it is possible in asp.net mvc 2 and if not then what is the way around for it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在尝试的事情是可能的,并且有不同的技术可以实现它。最好的方法取决于您如何调用控制器操作。无论您是使用普通超链接、提交标准
在您的控制器操作中,您可以拥有此附加参数:
另一个示例是,如果您使用 AJAX,您可以在母版页中设置默认数据,这将确保下拉列表的值将随您执行的每个 AJAX 请求一起发送:
现在,每当您向服务器发送 AJAX 请求时:
都会发送以下请求:
/foo?someParam=someValue&selectedGroup=123
,以便您能够取回所选组的值在控制器动作中。What you are trying is possible and there are different techniques to achieve it. The best approach would depend on how you are invoking your controller actions. Whether you are using normal hyperlinks, submitting standard
<form>
or using AJAX. So if you are using standard action links you could add some javascript which binds to the onclick event of each link and adds the selected value of the group. Example:and in your controller action you could have this additional parameter:
Another example is if you use AJAX you could setup a default data in the master page which will ensure that the value of the dropdown will be sent along each AJAX request you are performing:
Now whenever you send an AJAX request to your server:
the following request will be sent:
/foo?someParam=someValue&selectedGroup=123
so you will be able to fetch the value of the selected group back in the controller action.我希望我理解你的问题,因为没有提供代码示例。
您可以使用包含显示组的页面上的视图模型来执行此操作。另一种选择是在发布页面时从 FormCollection 获取值。
例如:
或者
如果这个答案不能让您满意,那么请更清楚地说明您的问题,我会尽力进一步帮助您。
I hope I understand your question since no code-example is supplied.
You can do this using a viewmodel on the page which contains your display groups. Another option is to get the value from the FormCollection when the page is posted.
For example:
OR
If this answer doesn't statisfy you then please be more clear in your question and i'll try to help you further.
只要您的下拉列表是提交的
最终呈现的 HTML:
控制器:
As long as your drop down list is a descendant of the
<form>
that is submitted, it will be subjected to standard model binding, which means you can include it as a parameter on your action. Ensure thename
attribute of the drop down list matches the name of the action parameter.Final rendered HTML:
Controller: