简体   繁体   English

获取下拉选择值并发送到asp.net核心web应用程序中同一页面的OnGet方法

[英]Get the dropdown selected value and send it to OnGet method of the same page in asp.net core web application

I am not from a developer background and I am trying to develop some utility which helps in automating some of our manual process and as part of that, I have designed a web application(simple web page).我不是开发人员背景,我正在尝试开发一些实用程序来帮助自动化我们的一些手动过程,作为其中的一部分,我设计了一个 web 应用程序(简单的 web 页面)。 I am able to populate the values in a dropdown using following code in.cshtml我可以使用以下代码 in.cshtml 填充下拉列表中的值

<select asp-for="SelectDD" asp-items=@Model.pvalues>
    <option value="" selected>Select a value..</option>
</select>

I thought of using a form in.cshtml which helps in reading the values easily but I am getting an error if I include html elements under a form.我想过使用 in.cshtml 中的表单来帮助轻松读取值,但是如果我在表单下包含 html 元素,则会出现错误。

Once I select a dropdown and clicking on a button calls the OnGet() method with the selected dropdown value using below code一旦我 select 下拉并单击按钮调用 OnGet() 方法并使用以下代码选择下拉值

<a id="pc" asp-page="Comparison" asp-route-ID="dropdownselectedvalue"><button type="button" class="btn btn-primary">Compare</button></a>

but the issue is I am not able to extract the selected dropdown value and not able to pass it through asp-route-ID attribute.但问题是我无法提取选定的下拉值,也无法通过 asp-route-ID 属性传递它。 Able to get the selected dropdown value using jquery in Console (Chrome developer tools) but couldn't find anything that helps me in sending the value with button click能够在控制台(Chrome 开发人员工具)中使用 jquery 获取选定的下拉值,但找不到任何可以帮助我通过单击按钮发送值的内容

but I am getting an error if I include html elements under a form.但如果我在表单下包含 html 元素,则会出现错误。

  • What is the error here?这里有什么错误?

  • What is the property name you want to submit?您要提交的房产名称是什么? is it SelectDD or ID ?SelectDD还是ID


Wrap the elements with a form tag and set the form method to get , and use a submit button:用表单标签包装元素并将表单方法设置为get ,并使用提交按钮:

<!-- use ID as the target property name in the select element -->
<form method="get">
    <select asp-for="ID" asp-items=@Model.pvalues>
        <option value="" selected>Select a value..</option>
    </select>
    <input type="submit" value="Compare" />
</form>

On the backend you can catch the value of the select either by using the parameter in get method:在后端,您可以使用 get 方法中的参数获取 select 的值:

public void OnGet(int ID)
{
    // ....
}

Or using a property name and adding binding attribute:或者使用属性名称并添加绑定属性:

[BindProperty(SupportsGet = true)]
public int ID { get; set; };

public void OnGet()
{
    // ...
}

Following helped me in resolving the issue.以下帮助我解决了这个问题。

I have modified the anchor tag as below (I have removed asp-route-ID attribute)我已经修改了锚标记如下(我已经删除了 asp-route-ID 属性)

<a id ="ps" asp-page="Comparison"><button type="button" class="btn btn-primary" onclick="getOption()">Compare</button></a>

I wrote the following javascript function to help me get the selected dropdown value and change the anchor tag href value我写了以下 javascript function 来帮助我获取选定的下拉值并更改锚标记href值

function getOption() {
    selectElement = document.querySelector('#SelectDD');
    output = selectElement.value;
    document.querySelector('#pc').setAttribute("href", "/Comparison?ID=" + output);
}

I spent 2-3 days on this but at last able to resolve.我花了2-3天的时间,但终于能够解决。 Hope this helps others希望这对其他人有帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试根据 ASP.NET Web 应用程序中的下拉列表和日期选择器中的选定值进行年龄验证 - Trying to do Age Validation based on selected value from dropdown list and datepicker in ASP.NET Web application 在 Asp.net core Mvc 中提交表单后,下拉列表未保留所选值 - Dropdown list is not retaining the selected value after submitting the form in Asp.net core Mvc 从ASP.NET页中的Web Api Controller获得价值 - Get value from Web Api Controller in ASP.NET page ASP.NET-刷新页面后不使用viewstate和autopostback保留下拉列表的选定值的状态 - ASP.NET - Keeping the state of the selected value of the dropdown after refreshing the page without using viewstate and autopostback JQuery datepicker 未通过在我的 asp.Net 核心应用程序中发布来发送选定的值 - JQuery datepicker not sending selected value by posting in my asp.Net core application 如何使用javascript在asp.net中的gridview中获取下拉列表的选定值? - How to get selected value of dropdown within gridview in asp.net using javascript? 工具提示文本作为ASP.Net下拉列表中的选定值 - ToolTip text as selected value in ASP.Net dropdown 如何获取DropDownList选定的值,并将其发送到ASP.NET MVC 4中的控制器并使用JavaScript? - How can I get a DropDownList selected value and send it to the controller in ASP.NET MVC 4 and using JavaScript? 使用JavaScript从Boostrap Dropdown列表中的数据库ASP.NET Web API获取价值 - Get value from database ASP.NET web api in Boostrap Dropdown list using JavaScript 我们是否必须将Ajax代码和Web方法放在同一asp.net页面中? - Do we have to put the ajax code and web method in the same asp.net page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM