2016-11-11 9 views
0

クライアント側からOnSelectedIndexChangedイベントを発生させたい場合は、JavaScriptからイベントを発生させてからサーバーサイドコードを呼び出します。 JavaScriptからOnSelectedIndexChangedイベントを発生させる方法

+0

javascriptのchangeイベントをdropdownlistで呼び出してtrueを返すと、サーバ側でも自動的に起動します。 – Adil

+0

[clientSideとServerSideのselectedIndexChangedドロップダウンリストの使い方]の可能な複製(http://stackoverflow.com/questions/12517227/how-use-selectedindexchanged-dropdownlist-in-clientside-and-serverside) – ScottyDoesKnow

+0

あなたはJavascriptのサーバーイベントですか? – Aruna

答えて

0

以下のようなドロップダウンののonchangeイベントであなたのクライアント側の関数名を追加します。

<asp:DropDownList ID="DropDownSubject" runat="server" DataTextField="Word" 
     AutoPostBack="True" OnSelectedIndexChanged="SelectedChange" 
     onchange="changeCursor()"> 
</asp:DropDownList> 

<script type="text/javascript"> 
function changeCursor() { 
    document.body.style.cursor="progress"; 
} 
</script> 
0
//declare Model in cshtml 

@model yourNameSpace.YourModelName 

// YourModelName.cs

public List<ClassName> ListOfObjects { get; set; } 


//Controller Level 

//call YourRepository return 
//List<ClassName> RepoMethodToGetListOfObjects(); 

var listOfObjects = YourRepository.RepoMethodToGetListOfObjects(); 

//then populate model 

model.ListOfObjects = listOfObjects 

//in your cshtml you will have 

    <td>@Html.DropDownList 
    ("test",new SelectList(Model.ListOfObjects, "objectId", "Description"), 
    new Dictionary<string, object> { { "id", "DropDownList" }, 
    {"onchange", "YourJavascriptFile.YourName.yourJavascriptFunctionName(this)" }})</td> 

// Javascript Function to get Value and Key back to controller 

YourName: { 

      yourJavascriptFunctionName: function(sel) { 
      //1way  var value = sel.value; 
       var key = $("#DropDownList option:selected").text(); 
       var selectedDropDownValue = getDropDownValue("DropDownList"); 
       location.href = "/YourFolderName/YourController/SelectedDropDownValueLogic?dropdownValue=" + 
       selectedDropDownValue + 
       "&dropdownKey=" + 
       key; 
      }, 
      }, 

      function getDropDownValue(name) { 
      return $("#" + name).val(); 
      } 

//際にドロップいくつかのより多くのコントローラロジックを実行します。下方向選択選択

public ActionResult SelectedDropDownValueLogic(string dropdownValue,string dropdownKey) 
    { 

     //Do Logic .. 
      Func<AggregateClass, ModelClass> buildModel = x => new ModelClass() 
       { 
       Property = x.Property, 
       }; 

        etc.....ForEach(x => model.ListOfObjects.Add(buildModel(x)) 
     //Do Logic .. 
+0

周囲の説明を追加するといいでしょう。 – zx485

関連する問題