簡體   English   中英

如何使用Ajax從列表框中發送選定的值

[英]How to send the selected values from list box using ajax

我需要使用MVC中的Ajax從列表框中發送多個選定的數據。 我得到選定的數據。 我不知道如何將數據ajax發送到控制器。 我的代碼如下:

var Students= [];

            var x = document.getElementById("ListBox");
            for (var i = 0; i < x.options.length; i++) {
                if (x.options[i].selected == true) {
                    Students.push(x.options[i].text)
                }
            }


 $.ajax({
                url: '',
                type: 'POST',
                data: { id:studenid, class: class,  Students: Students},
                dataType: 'json',
                success: function (data) {

                }
            }); // id:studenid, class:classs values are send it properly how to add the students?

您的url:''字段為空。 您需要在其中添加一些URL,您已經為其編寫了可以接受數據的服務器端代碼。

而且,當然,我猜測您希望發送data:{Student:Students}而不是data:{User:Users} 最好是data:{Student:Students}

//發送寬度參數名稱

   $.ajax({
      type: "POST",
      url: "Default.aspx/GetDate",
      data: { paramName: Students},
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(msg) {

      }
    });



  //Get value
    [WebMethod]
    public static string GetValue(string paramName)
      {

      }

在通用處理程序中

//Send
 $.ajax({
          type: "POST",
          url:'/MyHandler.ashx?students=Students,
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function(msg) {

          }
        });

//Get

public void ProcessRequest(HttpContext context)
{
    string students= (string)context.Request["students"];
}
var Students= [];

            var x = document.getElementById("ListBox");
            for (var i = 0; i < x.options.length; i++) {
                if (x.options[i].selected == true) {
                    Students.push(x.options[i].text)
                }
            }
$.ajax({
   type: "POST",
   data: {StudentList:Students},
   url: "index.aspx",
   success: function(msg){
     alert(msg);
   }
});

請確認您對控制器無效的“學生”對象的數據類型。

嘗試使用Array數據類型,

Public ActionResult SendData(string id, string class,Array  Students)
{

}

如果不起作用,則使用delimeter(,)將Javascript數組轉換為字符串數組,並在下面使用

Public ActionResult SendData(string id, string class,string Students)
{

}

這將為您提供幫助。 謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM