开发者

Can i access masterpage dropdown value in controller action in asp.net mvc

开发者 https://www.devze.com 2023-02-05 15:07 出处:网络
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 poss

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


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.


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.


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>
    ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消