2014-02-25 53 views
1

我的MVC表格沒有發佈Ajax下拉列表的參數。第一個dropdownlist工作正常,但第二個帶標籤的dropdownlist不會在我的代碼中發送任何參數,在調試中它仍然爲空。MVC表格沒有發佈Ajax下拉列表的參數

查看HTML:

@using ((Html.BeginForm("SuppliesForm", "Supplies", FormMethod.Post, new { id="myform"}))) 
{ 
<div class="labelme"> 
        <label for="BusinessClassID">ازکلاس</label> 
       </div> 
       @Html.DropDownListFor(model => model.BusinessClassID, Model.BusinessClassesList, new { id = "BusinessClassID" }) 
       <br /> 
       <select id="BusinessObjectID" name="BusinessObjectID"> 
        <option value="-1">به</option> 
       </select> 
} 

查看阿賈克斯和jQuery:

<script type="text/javascript"> 
    var c = null; 
    $(document).ready(function() { 
     $("#BusinessObjectID").prop("disabled", true); 
     $("#BusinessClassID").change(function() { 
      if ($("#BusinessClassID").val() == "d00717d5-ade6-4b4f-be0c-5e9b22f40391") 
      { 
       $(function() { 
        //var Data = JSON.stringify({ BusinessObjectID: $("#BusinessObjectID").val() }); 
        $.ajax({ 
         type: "POST", 
         url: BaseUrl + "/BPMS/api/BusinessList/list1", 
         //data: { BusinessObjectID: $("#BusinessObjectID").val() }, 
         contentType: "application/json; charset=utf-8", 
         dataType:"json", 
         success: function (data) { 
          var option1 = $('#BusinessObjectID'); 
          $(data).each(function (index, item) { 
           option1.append('<Option value="' + item.EventID + '">' + item.EventName + '</Option>').val(); 
          }) 
          $("#BusinessObjectID").prop("disabled", false); 
          $('#BusinessObjectID').append(option1); 
         } 
        }); 
       }); 
       } 
      }); 

的Web API:

public class Data 
     { 
      public Guid? id { get; set; } 

     } 
[System.Web.Http.HttpPost] 
     public List<Event> list1(Data p) 
     { 
      List<Event> EventList = Event.Query("Select * from Events").ToList(); 
      return EventList; 
     } 

控制器:

[HttpPost] 
     public ActionResult SuppliesForm(BusinessSupply supply) 
     { 
       try 
       { 
        //_BusinessSupplyList.Add(supply); 
        BusinessSupply al = new BusinessSupply(); 
        al.BusinessSupplyID = Guid.NewGuid(); 
        al.BusinessSupplyName = supply.BusinessSupplyName; 
        al.BusinessClassID = supply.BusinessClassID; 
        al.BusinessObjectID = supply.BusinessObjectID; 
        al.Insert(); 
        return RedirectToAction("Index"); 
       } 
+0

這種觀點有兩個下拉列表第一@ Html.DropDownListfor和謝勝利是阿賈克斯後成選擇標籤.....選擇標記,來自AJAX和jQuery不發送任何數據...在httppost仍null ... :( – user3266348

+0

您需要傳遞'數據p' –

回答

0

您錯過了您的ajax帖子的data參數。所以你的Data p在你的行動中是空的。

var postData={}; //similar to new Data(); 
postData.prop1=value1; 

    $.ajax({ 
        type: "POST", 
        url: BaseUrl + "/BPMS/api/BusinessList/list1", 
        data: postData, 
        contentType: "application/json; charset=utf-8", 
        dataType:"json", 
        success: function (data) { 
         var option1 = $('#BusinessObjectID'); 
         $(data).each(function (index, item) { 
          option1.append('<Option value="' + item.EventID + '">' + item.EventName + '</Option>').val(); 
         }) 
         $("#BusinessObjectID").prop("disabled", false); 
         $('#BusinessObjectID').append(option1); 
        } 
       });