title: Passing selected dropdown string value from table in view to controller
body: <p>I'm working on ASP.NET Core web application where I have a table in my view that displays all requests. each record with drop-down populated with all analysts successfully from my database, So the manager can assign the analyst from drop-down then approve the request.</p>
  <p>My questions:</p>
  <ol>
  <li><p>Can I implement this using form for each record instead using JavaScript, I mean using only asp tags?</p>
  </li>
  <li><p>If that should done using JavaScript, Here is my attempt to implement this.
  The following code is working only if the Analyst id is integer, but in my case the analyst id is string, so whenever I try to execute this, I got either &quot;null&quot; or &quot;Zero&quot; for the analyst id in the controller. Here is my ViewModel</p>
  </li>
  </ol>
  <pre class="lang-cs prettyprint-override"><code>public class RequestViewModel
  {
      public IEnumerable&lt;Request&gt; Requests { get; set; }
      public IEnumerable&lt;ApplicationUser&gt; AnalystList { get; set; }
      public Institution Institution { get; set; }
      public string selectedAnalyst { get; set; }
  }
  </code></pre>
  <p>Here is my controller</p>
  <pre class="lang-cs prettyprint-override"><code>      public async Task&lt;IActionResult&gt; ApproveRequest(int id, int Analystid)
              {
      
                  Request Req = await _db.Request
                      .Include(c =&gt; c.Institution)
                      .FirstOrDefaultAsync(c =&gt; c.Id == id);
      
                  if (Req.Type == SD.TypeRegister)
                  {
                      Req.Institution.Status = SD.StatusApproved;
                      Req.Institution.ApprovalDate = DateTime.Now;
                      Req.Institution.Seats = Req.Seats; // new
                      Req.Institution.AnalystId = Analystid.ToString(); //Here I want to get the id as string
      
                  }
                  else if (Req.Type == SD.TypeSeat)
                  {
                      Req.Institution.Seats += Req.Seats;
                  }
                  else if (Req.Type == SD.TypeSubscription)
                  {
                      Req.Institution.Seats = Req.Seats;
                      Req.Institution.Status = SD.StatusApproved;
                      Req.Institution.ApprovalDate = DateTime.Now;
                  }
      
                  Req.isDone = true;
                  await _db.SaveChangesAsync();
                  return await CreateApproval(id, SD.StatusApproved);
              }
  </code></pre>
  <p>Here is my View</p>
  <pre class="lang-cs prettyprint-override"><code>     @model TestApplication.Models.ViewModels.RequestViewModel
          @using TestApplication.Extensions
          @{
              ViewData[&quot;Title&quot;] = &quot;Index&quot;;
          }
      
              &lt;div class=&quot;tab-pane fade show active&quot; id=&quot;Register&quot; role=&quot;tabpanel&quot; aria-labelledby=&quot;Register-tab&quot;&gt;
      
                  Registration Requests 
                  &lt;div&gt;
                      @if (Model.Requests.Count() &gt; 0)
                      {
                          &lt;table class=&quot;table table-striped&quot;&gt;
                              &lt;tr class=&quot;table-secondary&quot;&gt;
                                  &lt;th&gt;
                                     Institution Name
                                  &lt;/th&gt;
                                  &lt;th&gt;
                                      Date
                                  &lt;/th&gt;
                                  &lt;th&gt;
                                      Actual seat
                                  &lt;/th&gt;
                                  &lt;th&gt;
                                      Seats
                                  &lt;/th&gt;
                                  &lt;th&gt;
                                     New Seat
                                  &lt;/th&gt;
                                  &lt;th&gt;
                                      Choose Analyst
                                  &lt;/th&gt;
                                  &lt;th&gt;
                                      Accept / Reject
      
                                  &lt;/th&gt;
                                  &lt;th&gt;
                                      Details
                                  &lt;/th&gt;
                                  &lt;th&gt;
                                  &lt;/th&gt;
                              &lt;/tr&gt;
                              @foreach (var item in Model.Requests)
                              {
                                  @if (item.Type == &quot;Register&quot; &amp;&amp; item.Institution.Status == &quot;Pending&quot;) @*need one*@
                                  {
                          &lt;tr&gt;
                              &lt;td&gt;
                                  @Html.DisplayFor(m =&gt; item.Institution.Name)
                              &lt;/td&gt;
                              &lt;td&gt;
                                  @Html.DisplayFor(m =&gt; item.Date)
                              &lt;/td&gt;
                              &lt;td&gt;
                                  @Html.DisplayFor(m =&gt; item.Institution.Seats)
                              &lt;/td&gt;
                              &lt;td&gt;
                                  @Html.DisplayFor(m =&gt; item.ActualSeats)
                              &lt;/td&gt;
                              &lt;td&gt;
                                  @Html.DisplayFor(m =&gt; item.Seats)
                              &lt;/td&gt;
                              &lt;td&gt;
                                  &lt;select id=&quot;selectedAnalyst_@item.Id&quot; asp-for=&quot;selectedAnalyst&quot; asp-items=&quot; Model.AnalystList.ToSelectListItem(Model.selectedAnalyst)&quot; class=&quot;form-control&quot;&gt;
                                         &lt;option selected value=&quot;&quot;&gt;--- Choose ---&lt;/option&gt;
                                  &lt;/select&gt;
                              &lt;/td&gt;
                              &lt;td&gt;
                                    &lt;a class=&quot;btn btn-info&quot; asp-controller=&quot;Request&quot; asp-action=&quot;ApproveRequest&quot; asp-route-id=&quot;@item.Id&quot;&gt; accept &lt;/a&gt;
                                    &lt;a class=&quot;btn btn-info&quot; asp-controller=&quot;Request&quot; asp-action=&quot;RejectRequest&quot; asp-route-id=&quot;@item.Id&quot;&gt; Reject &lt;/a&gt;
                              &lt;/td&gt;
                              &lt;td&gt;
                                  &lt;button type=&quot;submit&quot; class=&quot;btn btn-success anchorDetail&quot; data-target=&quot;#modal-@item.Institution.Id&quot; data-toggle=&quot;modal&quot;&gt;
                                      View Details
                                  &lt;/button&gt;
                              &lt;/td&gt;
                              &lt;td&gt;
                                  &lt;div class=&quot;modal fade&quot; id=&quot;modal-@item.Institution.Id&quot; tabindex=&quot;-1&quot; role=&quot;dialog&quot; aria-hidden=&quot;true&quot;&gt;
                                      &lt;div class=&quot;modal-dialog-centered modal-dialog&quot; role=&quot;document&quot;&gt;
                                          &lt;div class=&quot;modal-content&quot;&gt;
                                              &lt;div class=&quot;modal-header bg-success text-light justify-content-center&quot;&gt;
                                                  &lt;h5 class=&quot;modal-title&quot;&gt;Request Details&lt;/h5&gt;
                                              &lt;/div&gt;
                                              &lt;div class=&quot;modal-body justify-content-center&quot; id=&quot;MyModalContent&quot;&gt;
                                                  @await Html.PartialAsync(&quot;_RequestDetails&quot;, item)
                                              &lt;/div&gt;
                                              &lt;div class=&quot;modal-footer&quot;&gt;
                                                  &lt;button type=&quot;button&quot; class=&quot;btn btn-secondary&quot; data-dismiss=&quot;modal&quot;&gt;إغلاق&lt;/button&gt;
                                              &lt;/div&gt;
                                          &lt;/div&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                                  }
                              }
                          &lt;/table&gt;
                      }
                      else
                      {
                          &lt;p&gt;No Institutions Exists...&lt;/p&gt;
                      }
                  &lt;/div&gt;
      
      
              &lt;/div&gt;
      @section scripts
      {
          &lt;script&gt;
      
              function accept(id) {
                  var aid = $('#selectedAnalyst_' + id).val()
                  location.href = &quot;/Request/ApproveRequest?id=&quot; + id + &quot;&amp;Analystid=&quot; + aid
              }
      
      
      
              var PostBackURL = '/Request/RequestDetails';
              $(function () {
                  $(&quot;.anchorDetail&quot;).click(function () {
                      var $buttonClicked = $(this);
                      var id = $buttonClicked.attr('data-id');
                      $.ajax({
                          type: &quot;GET&quot;,
                          url: PostBackURL,
                          contentType: &quot;application/json; charset=utf-8&quot;,
                          data: { &quot;Id&quot;: id },
                          cache: false,
                          datatype: &quot;json&quot;,
                          success: function (data) {
                              $('#MyModalContent').html(data);
                              $('#myModal').modal('show');
                          },
                          error: function () {
                              alert(&quot;Dynamic content load failed.&quot;);
                          }
                      });
                  })
          &lt;/script&gt;
      }
      
      &lt;div class=&quot;modal fade&quot; id=&quot;MyModal&quot; tabindex=&quot;-1&quot; role=&quot;dialog&quot;
           aria-labelledby=&quot;myModalLabel&quot;&gt;
          &lt;div id='MyModalContent'&gt;&lt;/div&gt;
      &lt;/div&gt;
  </code></pre>