using ajax with dropdownlist mvc3 [closed]

纵饮孤独 提交于 2019-11-27 16:49:53

问题


Is there any way to refresh some part of page (e.g div/span) on selection of dropdownlist option ... ?? Please note I'm using razor syntax.

If yes, then please give some sample code.


回答1:


Yes, you can subscribe to the onchange event.

@Html.DropDownListFor(m => m.ItemId, Model.ItemList, "Select an item...", new { onchange = "somefunction();" })

Maybe like this (real example):

   @using (Ajax.BeginForm("Action", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "divtoupdate", InsertionMode = InsertionMode.Replace }))
    {
        @Html.DropDownListFor(m => m.ItemId, Model.ItemList, "Select an item...", new { onchange = "doSubmit($(this).parents('form'));" })
    }

And then have this javascript function (or similar)

<script>
function doSubmit(form){
  // event.preventDefault(); doesn't work in IE8 so do the following instead
  (event.preventDefault) ? event.preventDefault() : event.returnValue = false;
  form.submit();
}
</script>

EDIT: This example assumes you are using unobtrusive validation (and therefore jQuery) and want to submit a form, but you could obviously call any javascript function for the onchange event and do whatever you want...




回答2:


just add some javascript/jquery to your code. somthinglike this.

$("#button").click(function(){

$("#div").load("www.wateveryourdatapageis.com");

});


来源:https://stackoverflow.com/questions/8986795/using-ajax-with-dropdownlist-mvc3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!