简体   繁体   中英

passing dropdown list value from View to controller

I created a dropdown list in my view inside "Requests Table" where it populates data as "Analysts list" from my database successfully.

But I'm stuck on how I can pass the selected value from the dropdown list inside the table when clicking the "Accept" button to my controller.

RequestViewModel .

public class RequestViewModel
{
    public IEnumerable<Request> Requests { get; set; }
    public IEnumerable<ApplicationUser> AnalystList { get; set; }
    public Institution Institution { get; set; }
    public string selectedAnalyst { get; set; }
}

RequestController

 public async Task<IActionResult> ApproveRequest(int id) 
{
    <--------- Lines of Code ---------> 
    if (Req.Type == SD.TypeRegister)
    {
        Req.Institution.Status = SD.StatusApproved;
        Req.Institution.ApprovalDate = DateTime.Now;
        Req.Institution.Seats = Req.Seats; // new
        Req.Institution.AnalystId = ;*//Here i want to set the value to the AnalystId from dropdown list*
    }
    <--------- Lines of Code ---------> 
}
 

IndexView

<--------- Lines of Code ---------> 
<table>
    @foreach (var item in Model.Requests)
    {
        @if (item.Type == "Register" && item.Institution.Status == "Pending") 
        {
            <tr>
                <td>
                    @Html.DisplayFor(m => item.Institution.Name)
                </td>
                <td>
                    @Html.DisplayFor(m => item.Date)
                </td>
                <td>
                    <select asp-for="selectedAnalyst" asp-items="Model.AnalystList.ToSelectListItem(Model.selectedAnalyst)" class="form-control">
                        <option selected value="">--- Choose ---</option>
                    </select>
                </td>
                <td>
                    <a class="btn btn-info" asp-controller="Request" asp-action="ApproveRequest" asp-route-id="@item.Id"> accept </a>
                    <a class="btn btn-info" asp-controller="Request" asp-action="RejectRequest" asp-route-id="@item.Id"> Reject </a>
                </td>
                <td>
                    <button type="submit" class="btn btn-success anchorDetail" data-target="#modal-@item.Institution.Id" data-toggle="modal">
                       View details
                    </button>
                </td>

                 <--------- Lines of Code ---------> 
            </tr>
        }
    }
</table>

    <--------- Lines of Code ---------> 

@section scripts
{
    <script>
        var PostBackURL = '/Request/RequestDetails';
        $(function () {
            $(".anchorDetail").click(function () {
                <--------- Lines of Code ---------> 
            })
    </script>
}

Can I pass it the same way I did in the "RequestDetails" script but to a different action?

Update your button to something like this

<input type="button" class="btn btn-success anchorDetail" data-target="#modal-@item.Institution.Id" data-toggle="modal" onclick="location.href='@Url.Action("ApproveRequest", "Request", new { id = yourvalue })'" />

And update the definition of your controller to this:

public async Task<IActionResult> ApproveRequest(string id)

You only give the url and click event, but don't give a specific implementation method. So I can't understand your specific meaning. If you only want to pass the id of the current row and the value of the current drop-down list to RequestController, you can refer to the following implementation, modify its id:

   <td>
        <select id="selectedAnalyst_@item.Id" asp-for="selectedAnalyst" asp-items=" Model.AnalystList.ToSelectListItem(Model.selectedAnalyst)" class="form-control">
          <option selected value="">--- Choose ---</option>
        </select>
</td>

Add a click function on accept button:

 <td>
        <a class="btn btn-info" onclick="accept(@item.Id)" > accept </a>
   </td>

The script:

  @section Scripts
{
        <script>
           function accept(id) {
                var aid = $('#selectedAnalyst_' + id).val()
                location.href = "/Request/ApproveRequest?id=" + id + "&Analystid=" + aid
            }
        </script>
}

Here is ApproveRequest:

public IActionResult ApproveRequest(int id,int Analystid)
        {
            return Json($"{id} {Analystid}");
    }

result: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM