C# 如何在不使用表单post的情况下从控制器视图中的SelectList中获取所选值?

C# 如何在不使用表单post的情况下从控制器视图中的SelectList中获取所选值?,c#,routes,attributes,asp.net-core-mvc,C#,Routes,Attributes,Asp.net Core Mvc,我正试图将所选选项项的值作为参数传递给我的控制器。我没有使用表单,因为我的视图使用了一个表,它不喜欢表中的表单。我需要知道我的控制器方法中的companyID、userID和RoleID来完成这项工作。我正在正确获取companyID和userID,但我不知道如何获取用户选择的角色 视图: @DisplayNameFor(model=>model.FirstName) @DisplayNameFor(model=>model.LastName) @DisplayNameFor(model=>m

我正试图将所选选项项的值作为参数传递给我的控制器。我没有使用表单,因为我的视图使用了一个表,它不喜欢表中的表单。我需要知道我的控制器方法中的companyID、userID和RoleID来完成这项工作。我正在正确获取companyID和userID,但我不知道如何获取用户选择的角色

视图:


@DisplayNameFor(model=>model.FirstName)
@DisplayNameFor(model=>model.LastName)
@DisplayNameFor(model=>model.Email)
@DisplayNameFor(model=>model.Status)
@DisplayNameFor(model=>model.RegistrationDate)
帐户类型
行动
@foreach(模型中的var项目)
{
@DisplayFor(modelItem=>item.FirstName)
@DisplayFor(modelItem=>item.LastName)
@DisplayFor(modelItem=>item.Email)
@DisplayFor(modelItem=>item.Status)
@DisplayFor(modelItem=>item.RegistrationDate)
@DisplayNameFor(model=>model.Role)

当我点击“批准”按钮时,我没有在url中添加角色ID

我在_layout.cshtml页面中包含了标准jquery脚本标记:

  <script src="~/lib/jquery/dist/jquery.min.js"></script>

运行项目时,标记帮助程序将在
锚定中生成
href
。因此无法通过js设置标记帮助程序值。您需要设置
href

视图:

/。。。。
@DisplayNameFor(model=>model.Role)

更新:

您不工作的原因是我们有不同的razor视图。请按以下方式更改代码:

                <td>
                    <select onchange="SetRoleId(this)" id="roleList" name="roleList" asp-items="@(new SelectList(@ViewBag.message, "ID", "Name"))">
                        <option selected="selected" value="">
                            @Html.DisplayNameFor(model => model.Role)
                        </option>
                    </select>
                </td>
                <td>
                    <a id="approve" asp-action="Approve" asp-route-companyID="@item.CompanyID" asp-route-id="@item.Id">Approve</a> |
                    <a asp-action="Reject" asp-route-companyID="@item.CompanyID" asp-route-id="@item.Id">Reject</a>
                </td>

            </tr>
        }
        </tbody>
    </table>
@section scripts
{
    <script>   
        function SetRoleId(data) {
            var id = data.value;
            if (id > 0) {
                var element = $(data).parent().next("td").find("#approve");
                var url = element.attr("href");
                var list = url.split("/");
                var index = list.length;
                if (index < 6) {
                    element.attr("href",url+ "/" +id);                                       
                }
                else {
                    var urlstring = ""; 
                    for (var i = 1; i < index - 1; i++) {
                        urlstring = urlstring+ "/" + list[i] ;
                    }
                    element.attr("href", urlstring+"/"+id);
                }
            }
            else {
                $('#message').text('select an option first!')
            }                   
        };
    </script>
}

@DisplayNameFor(model=>model.Role)
批准|
拒绝
}
@节脚本
{
函数SetRoleId(数据){
var id=data.value;
如果(id>0){
var元素=$(数据).parent().next(“td”).find(“批准”);
var url=element.attr(“href”);
var list=url.split(“/”);
var指数=list.length;
如果(指数<6){
attr(“href”,url+“/”+id);
}
否则{
var urlstring=“”;
对于(变量i=1;i
首先,请花点时间对代码进行适当的格式化。其次,
asp-route
在服务器端工作,如果页面尚未呈现给客户端,则无法获取客户端值页面已呈现,角色在下拉列表中呈现。现在我正试图将所选角色值发送给控制器。这不对吗?不,您需要对表单进行subit,或者使用JavaScriptI发出HTTP请求。我没有使用表单,因为它不喜欢我的表中的表单。这个路由看起来对我很有效,只是我缺少了下拉列表中选择的roleID。似乎我可以使用ATHtml.ActionLink(“Approve”、“Admin”、new{ATitem.CompanyID、ATitem.Id、})将我的视图参数发送给控制器进行处理。但是,我不知道如何获得所选角色。请向我们共享足够的代码和错误消息截图。我用您的有用代码更新了原始帖子。浏览器控制台中的错误现在是“Uncaught TypeError:无法读取未定义的属性'split'”您能接受我的回答吗?它还可以帮助其他人快速找到解决方案。请参阅:。谢谢。
  <td>
    <select onchange="SetRoleId(this)" id="roleList" name="roleList" asp-items="@(new SelectList(@ViewBag.message, "ID", "Name"))">

      <option selected="selected" value="">@Html.DisplayNameFor(model => model.Role)</option>
     </select>

            </td>
            <td>
                <a id="approve" asp-action="Approve" asp-route-companyID="@item.CompanyID" asp-route-id="@item.Id">Approve</a> |
                <a asp-action="Reject" asp-route-companyID="@item.CompanyID" asp-route-id="@item.Id">Reject</a>
            </td>
        </tr>
    }
</tbody>
@section scripts {
    <script>
    function SetRoleId(data) {
        var id = data.value;
        if (id > 0) {
            var url = $(data).next("#approve").attr("href");
            var list = url.split("/");
            var index = list.length;
            if (index < 6) {
                //why is 6
                //the default length of your approve link is 5:"","Admin","Approve","companyID" and "id"
                //you just need to add the roleid to the url ending
                $(data).next("#approve").attr("href", url + "/" + id);
            }
            else {
                //if you want to change the selectlist,the default url length is 6
                //because you have added the roleid before
                var urlstring = "";
                for (var i = 1; i < index - 1; i++) {
                    urlstring = urlstring + "/" + list[i];
                }
                $(data).next("#approve").attr("href", urlstring + "/" + id);
            }
        }
        else {
            $('#message').text('select an option first!')
        }
    };
</script>
}
  <script src="~/lib/jquery/dist/jquery.min.js"></script>
                //....
                <td>
                    <select onchange="SetRoleId(this)" id="roleList" name="roleList" asp-items="@(new SelectList(@ViewBag.message, "ID", "Name"))">
                        <option selected="selected" value="">
                            @Html.DisplayNameFor(model => model.Role)
                        </option>
                    </select>

                    <a id="approve" asp-action="Approve" asp-route-companyID="@item.CompanyID" asp-route-id="@item.Id">Approve</a> |
                    <a asp-action="Reject" asp-route-companyID="@item.CompanyID" asp-route-id="@item.Id">Reject</a>
                 </td>
            </tr>
        }
        </tbody>
    </table>
@section scripts
{
    <script>   
        function SetRoleId(data) {
            var id = data.value;
            if (id > 0) {               
                var url = $(data).next("#approve").attr("href");
                var list = url.split("/");
                var index = list.length;
                if (index < 6) {
                    //why is 6
                    //the default length of your approve link is 5:"","Admin","Approve","companyID" and "id"
                    //you just need to add the roleid to the url ending
                    $(data).next("#approve").attr("href",url+ "/" +id);                                       
                }
                else { 
                   //if you want to change the selectlist,the default url length is 6
                   //because you have added the roleid before
                    var urlstring = ""; 
                    for (var i = 1; i < index - 1; i++) {
                        urlstring = urlstring+ "/" + list[i] ;
                    }
                    $(data).next("#approve").attr("href", urlstring+"/"+id);
                }
            }
            else {
                $('#message').text('select an option first!')
            }                   
        };
    </script>
}
                <td>
                    <select onchange="SetRoleId(this)" id="roleList" name="roleList" asp-items="@(new SelectList(@ViewBag.message, "ID", "Name"))">
                        <option selected="selected" value="">
                            @Html.DisplayNameFor(model => model.Role)
                        </option>
                    </select>
                </td>
                <td>
                    <a id="approve" asp-action="Approve" asp-route-companyID="@item.CompanyID" asp-route-id="@item.Id">Approve</a> |
                    <a asp-action="Reject" asp-route-companyID="@item.CompanyID" asp-route-id="@item.Id">Reject</a>
                </td>

            </tr>
        }
        </tbody>
    </table>
@section scripts
{
    <script>   
        function SetRoleId(data) {
            var id = data.value;
            if (id > 0) {
                var element = $(data).parent().next("td").find("#approve");
                var url = element.attr("href");
                var list = url.split("/");
                var index = list.length;
                if (index < 6) {
                    element.attr("href",url+ "/" +id);                                       
                }
                else {
                    var urlstring = ""; 
                    for (var i = 1; i < index - 1; i++) {
                        urlstring = urlstring+ "/" + list[i] ;
                    }
                    element.attr("href", urlstring+"/"+id);
                }
            }
            else {
                $('#message').text('select an option first!')
            }                   
        };
    </script>
}