" View 中获取 "ActionResult"的值?-6ren"> " View 中获取 "ActionResult"的值?-我有一个标签和一个按钮。单击按钮时,将调用 Controller 中的 actionResult。像这样。 Get Items 这是 ActionResult public ActionResult -6ren">
gpt4 book ai didi

javascript - 如何从 Controller 的 "" View 中获取 "ActionResult"的值?

转载 作者:行者123 更新时间:2023-11-29 23:08:34 25 4
gpt4 key购买 nike

我有一个标签和一个按钮。单击按钮时,将调用 Controller 中的 actionResult。像这样。

<button type="button" onclick="location.href='@Url.Action("GetAllItems", "Items")'">Get Items</button>

这是 ActionResult

public ActionResult GetAllItems() {
string selectedItem = string.Empty;
ItemsRepository itemsRepository = new ItemsRepository();
ModelState.Clear();
return View(itemsRepository.GetAllItems());
}

selectedItem是我要存储 selectedText 或 selectedValue 的变量。但我不知道该怎么做。

这是 <Select>标签。

<select style="width:170px" id="ItemsID" name="Items"></select>

这是从 onChange 得到不同结果的rough 函数事件。

<script>
$(document).ready(function () {
$("#ItemsID").change(function () {
var a = $('#ItemsID option:selected').val();
var b = $('#ItemsID option:selected').text().trim();
var c = $('#ItemsID option:selected').index();

alert(a);
alert(b);
alert(c);
})
});

请帮忙。

最佳答案

location.href 没有发送 POST 请求。您要做的是在 ActionResult 中放置一个表单标签以发出 POST 请求。

<form action='yourAction'>
<select name='name'></select>
<button type="submit">Get Items</button>
</form>

关于javascript - 如何从 Controller 的 "<select></select>" View 中获取 "ActionResult"的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54283360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com