我想从客户端触发OnSelectedIndexChanged事件,当我们从javascript触发该事件时,服务器端代码将调用。
答案 0 :(得分: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>
答案 1 :(得分: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 ..