[英]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.