Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

asp.net - get the select element in .NET using AJAX

I have ajax function like this to run on HTML select list

$.ajax({
                type: "POST",
                url: urlemp,
                success: function (returndata) {
                    if (returndata.ok) {
                        //  var data = eval("" + returndata.data + "");
                        select.empty();
                        select.append($('<option>' + "" + '</option>'));
                        $.each(returndata.data, function (rec) {

                            select.append($('<option>' + returndata.data[rec].Name + '</option>'));
                        });
                        select.show('slow');
                        select.change();
                    }
                    else {
                        window.alert(' error : ' + returndata.message);
                    }

                }
            }
    );

and this is the HTML element

        <select id="cmbDept"></select>

How can i get the value of the selected item in the controller using MVC 3 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you have 4 ways to do that
1. the you can bind ti the change event of the select $(select).change(function(){}) and send an ajax request again wrapping the selected value which you will be able to get in the controller

2. you can keep a hidden input in your view binded to a property in the view's model now bind to the change of the select and fill the input with the value this way whenever your form is posted back it will have the values properly binded to the model

3. @Don saved me from writing the third way so read his ans.

4. if you have a model that this view is binded to then simple keep a property in the model with the name cmbDept and selected value would be automatically posted back


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...