Search code examples
asp.netrazorforeachviewbag

how to get the selected value from a drop down list and transfer it to a viewbag


View

...Some Codes...
<select class="form-control" style = "width:400px"> 
    <option selected> select issue type </option>
    @foreach( var item in  Model._issueList)
    {
        <option>@item.issueName</option>
    }
</select>
...Some Codes...

Output of my dropdown

dropDown: issue_one, issue_two, issue_three

Now I want to choose my issue_three in my drop down and save it in a Viewbag.select, want should be the proper code so that I can transfer the issue_three to my Viewbag.select

TEST

...foreach code...
Viewbag.select = ????

Solution

  • View

    <select class="form-control" style = "width:400px"> 
    <option selected> Select Company Name </option>
    @foreach (var item in Model._issueList)
    {
        <option id="issueSelect" onclick="transferIssue('@item.issueNo')">@item.issueName</option> 
    }
    </select>
    @Html.HiddenFor( m => m.variable,new { @class = "form-control", style = "width:400px", @id = "issue" })
    ...Some codes...
    <button type ="submit" id='submit_button'>Submit</button>
    

    SCRIPT Use JQUERY

    function transferIssue(x){ $('#issue')val(); }
    
    jQuery(document).on('click', '#submit_button', function (ev) {
        ev.preventDefault();
        var _issue = jQuery('#issue').val();
        var _url = '@Url.Action("Jobs")' + '?value='+ _issue;
    
    window.location.href = _url;
    
    });