Sending form with Select List

强颜欢笑 提交于 2019-12-25 03:18:19

问题


I am using this code to post value of selected item from the view to the controller

@using (Html.BeginForm())
{
        @Html.DropDownListFor(model => model.MyVar, (SelectList)ViewData["List"])
        <button name="Button" value="Valider">Valider</button>

}

Is there a way to send the value when the selection change in the select list (without the need to click on the button) ?


回答1:


yes you can do it via JQUERY, on dropdown selection change post the form via jquery:

add id to drop down:

@Html.DropDownListFor(model => model.MyVar, (SelectList)ViewData["List"], new { id="SomeId"})

and write jquery event:

$(function(){

    $("#SomeId").change(function(){

        $(this).closest("form").submit(); // this will post the form
    });   

});



回答2:


If you name the SelectList in the ViewData the same as the name of the variable in your Model, MVC will figure the rest out for itself.

So your dropdown would look like:

@Html.DropDownList(ViewData.MyVar, String.Empty)

This is as opposed to naming your ViewData item 'List'.



来源:https://stackoverflow.com/questions/25361816/sending-form-with-select-list

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