0

ドロップダウンからトリガーされる一連のデータを戻すための単純なコードセットが本当にあります。

これはスクリプトです:

function () {
   $('#ProviderID').change(function () {

    $.ajax({
        url: '/servicesDisplay/Index',
        type: 'Get',
        data: { id: $(this).attr('value') },
        success: function (result) {
            // The AJAX request succeeded and the result variable
            // will contain the partial HTML returned by the action
            // we inject it into the div:
            $('#serLocations').html(result);
        }
    });
}); 

これはコントローラーです:

 public ActionResult Index(string id)
 {
     int prid = Int32.Parse(id.Substring(0, (id.Length-1))); 
     string mulitval = id.Substring((id.Length-1), 1).ToString();
     System.Data.Objects.ObjectResult<getProviderServiceAddress_Result> proList = theEntities.getProviderServiceAddress(prid);
     List<getProviderServiceAddress_Result> objList = proList.ToList();
     SelectList providerList = new SelectList(objList, "AddressID","Address1");
     //ViewBag.providerList = providerList;
     return PartialView("servicesDisplay/Index", providerList);
 }

これはビューです:

@model OCS_CS.Models.servicesDisplay

  <div>
    @Html.DropDownList(model => model.ServiceAdderssID, (IEnumerable<SelectListItem>)model)
 </div>   

ドロップダウンが値を渡​​すとき。アプリはコントローラーにヒットします。ただし、ドロップダウンが明るい赤で強調表示され、ビューが表示されません。

4

3 に答える 3

0

GET を行っています。データを ajax に渡す意味はありません。POST にデータを渡すことができます。

まず、URL に値を入力します。

function () {
 $('#ProviderID').change(function () {

 $.ajax({
    url: '/servicesDisplay/Index/' + $(this).attr('value'),
    type: 'Get',
    success: function (result) {
        // The AJAX request succeeded and the result variable
        // will contain the partial HTML returned by the action
        // we inject it into the div:
        $('#serLocations').html(result);
    }
  });
});

次に、メソッドを GET としてマークします

 [HttpGet]
 public ActionResult Index(string id)

これがお役に立てば幸いです。

于 2013-06-21T19:14:44.810 に答える
0

loadjqueryメソッドを使用するこの短いバージョンを試してください。

$(function(){

   $('#ProviderID').change(function () {
      $('#serLocations').load("@Url.Action("Index","ServicesDisplay")?id="
                                                              +$(this).val());   
    });

}); 

結果のキャッシュを回避したい場合は、一意のタイムスタンプをクエリ文字列と共に送信して、キャッシュを回避できます。

$('#serLocations').load("@Url.Action("Index","ServicesDisplay")?id="
                                                  +$(this).val()+"&t="+$.now()); 
于 2013-06-21T19:21:36.127 に答える
0

コードにかなりの問題があります。まず、ビュー用に定義されたモデルは次のとおりです。

@model OCS_CS.Models.servicesDisplay

しかし、あなたのアクションでは、SelectList を渡してこのビューへの呼び出しを呼び出しています。

SelectList providerList = new SelectList(objList, "AddressID","Address1");
return PartialView("servicesDisplay/Index", providerList);

モデルがタイプごとに一致しないため、これは飛行しません。秒の問題は、この SelectList を IEnumerable にキャストしていることです。これもうまくいきません。SelectList にキャストする必要があります。

@Html.DropDownList(model => model.ServiceAdderssID, (SelectList)model)

ただし、アクションのモデルのタイプをビューのモデルと一致させるまで、これは機能しません。Fiddler をインストールして、発生しているエラーの種類を特定することをお勧めします。

于 2013-06-21T19:44:44.450 に答える