我可以在 asp.net mvc 的控制器操作中访问母版页下拉值吗

发布于 2024-10-12 09:21:57 字数 97 浏览 1 评论 0原文

我有一个母版页,在上面显示用户可以访问的组,现在我想在许多控制器中获取其选定的值以与记录一起保存。我想知道在 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 技术交流群。

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

发布评论

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

评论(3

梦太阳 2024-10-19 09:21:57

您正在尝试的事情是可能的,并且有不同的技术可以实现它。最好的方法取决于您如何调用控制器操作。无论您是使用普通超链接、提交标准

还是使用 AJAX。因此,如果您使用标准操作链接,您可以添加一些 javascript,它绑定到每个链接的 onclick 事件并添加该组的选定值。示例:

$(function() {
    $('a').click(function() {
        // everytime a link is clicked
        // add the value of the selected group to the url
        var selectedGroup = $('#IdOfYourDdl').val();
        if (this.href.indexOf('?') > 0) {
            window.location = this.href + '&selectedGroup=' + selectedGroup;
        } else {
            window.location = this.href + '?selectedGroup=' + selectedGroup;
        }
        // cancel the default action as it doesn't contain the selectedGroup
        // parameter in the request
        return false;
    });
});

在您的控制器操作中,您可以拥有此附加参数:

public ActionResult Foo(string selectedGroup)
{
   ...
}

另一个示例是,如果您使用 AJAX,您可以在母版页中设置默认数据,这将确保下拉列表的值将随您执行的每个 AJAX 请求一起发送:

$.ajaxSetup({
    data: { selectedGroup: $('#IdOfYourDdl').val() }
});

现在,每当您向服务器发送 AJAX 请求时:

$.get('/foo', { someParam: 'someValue' }, function(result) {

});

都会发送以下请求:/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:

$(function() {
    $('a').click(function() {
        // everytime a link is clicked
        // add the value of the selected group to the url
        var selectedGroup = $('#IdOfYourDdl').val();
        if (this.href.indexOf('?') > 0) {
            window.location = this.href + '&selectedGroup=' + selectedGroup;
        } else {
            window.location = this.href + '?selectedGroup=' + selectedGroup;
        }
        // cancel the default action as it doesn't contain the selectedGroup
        // parameter in the request
        return false;
    });
});

and in your controller action you could have this additional parameter:

public ActionResult Foo(string selectedGroup)
{
   ...
}

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:

$.ajaxSetup({
    data: { selectedGroup: $('#IdOfYourDdl').val() }
});

Now whenever you send an AJAX request to your server:

$.get('/foo', { someParam: 'someValue' }, function(result) {

});

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.

三生一梦 2024-10-19 09:21:57

我希望我理解你的问题,因为没有提供代码示例。
您可以使用包含显示组的页面上的视图模型来执行此操作。另一种选择是在发布页面时从 FormCollection 获取值。

例如:

public ActionResult MyAction(MyViewmodel viewModel)
{
//value from your viewmodel, strongtype
var value = viewModel.group;
}

或者

public ActionResult MyAction(FormCollection collection)
{
//value as a string
var value = collection.getValue("MyKey").AttemptedValue;
}

如果这个答案不能让您满意,那么请更清楚地说明您的问题,我会尽力进一步帮助您。

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:

public ActionResult MyAction(MyViewmodel viewModel)
{
//value from your viewmodel, strongtype
var value = viewModel.group;
}

OR

public ActionResult MyAction(FormCollection collection)
{
//value as a string
var value = collection.getValue("MyKey").AttemptedValue;
}

If this answer doesn't statisfy you then please be more clear in your question and i'll try to help you further.

白云不回头 2024-10-19 09:21:57

只要您的下拉列表是提交的

的后代,它将受到标准 模型绑定,这意味着您可以将其作为参数包含在操作中。确保下拉列表的 name 属性与操作参数的名称匹配。

最终呈现的 HTML:

<form action="http://.../MyAction" method="post">
  <select name="dropDownList">
    <option value="a">Option A</option>
    <option value="b">Option B</option>
    ...
  </select>
  ...
</form>

控制器:

[HttpPost]
public ActionResult MyAction(string dropDownList, MyActionModel model)
{
    // dropDownList = value attribute of selected <option>
    ...
}

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 the name attribute of the drop down list matches the name of the action parameter.

Final rendered HTML:

<form action="http://.../MyAction" method="post">
  <select name="dropDownList">
    <option value="a">Option A</option>
    <option value="b">Option B</option>
    ...
  </select>
  ...
</form>

Controller:

[HttpPost]
public ActionResult MyAction(string dropDownList, MyActionModel model)
{
    // dropDownList = value attribute of selected <option>
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文