将下拉列表值从View传递到控制器

时间:2020-09-01 19:23:31

标签: c# asp.net asp.net-mvc asp.net-core .net-core

我在“请求表”内的视图中创建了一个下拉列表,该列表成功地从数据库中将数据填充为“分析师列表”。

但是,当我单击控制器上的“接受”按钮时,我仍然无法传递表格中下拉列表中的选定值。

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>
}

我可以像在“ RequestDetails”脚本中一样传递它,但可以传递不同的动作吗?

2 个答案:

答案 0 :(得分:0)

将您的按钮更新为类似的内容

 p1 + geom_hline(yintercept = 1, color = "black")  

并将控制器的定义更新为此:

<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 })'" />

答案 1 :(得分:0)

您仅提供url和click事件,但没有提供具体的实现方法。因此我无法理解您的具体含义。 如果只想将当前行的ID和当前下拉列表的值传递给RequestController,则可以参考以下实现,修改其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>

在接受按钮上添加点击功能:

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

脚本:

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

这是批准请求:

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

结果: enter image description here

相关问题