0

I have this view

@model IEnumerable<Helpdesk.Infrastructure.Tools.IdNameSurname>

<div class="col-lg-12">
        <div class="user-select">
            <span style="display: inline-block">Show timesheet for user</span>
            <div class="user-select-dropdown">
                <select class="form-control">
                    <option value="" disabled selected>Select user...</option>
                    @Url.Action("Timesheet", "Administrator")
                    @foreach (var users in Model)
                    {
                        <option value="@users.Id">@users.Name @users.Surname</option>
                    }
                </select>
            </div>
        </div>
        @{
            Html.RenderPartial("PartialTimesheet", new { userId = ??? });
        }
    </div>

How I can assign my select option value to my RenderPartial view?

HUSTLIN
  • 196
  • 4
  • 17
  • 2
    You cant. `Html.RenderPartial` is generated on the server before its sent to the client. If you want to respond to client side events, you need javascript and ajax to call a controller method that returns a partial view –  Sep 21 '15 at 11:40

1 Answers1

2

Basically, you can't do it. The reason is: Razor syntax is a server side code, it will turn into HTML before returning to the client.

So, the correct way to do this is using javascript. Here are the steps:

  1. Prepare your controller. It must return a PartialView.
  2. In your javascript code, get the value of dropdownlist.
  3. Make an Ajax call to your PartialView controller to get the appropriate view.
  4. Display the view.

For the code, you can look here. It's for radio button, but for dropdownlist is the same.

Community
  • 1
  • 1
Triet Doan
  • 11,455
  • 8
  • 36
  • 69
  • ok, my partial view will be writed on angularjs, but how I can get the value of my dropdownlist? – HUSTLIN Sep 21 '15 at 12:14
  • Use this: `$('#dropDownId :selected').text();`. Here is the [link](http://stackoverflow.com/questions/2780566/get-selected-value-of-a-dropdowns-item-using-jquery) to the answer. – Triet Doan Sep 21 '15 at 13:22