javascript - 如何获得“” 从视图到控制器的“ActionResult”?
问题描述
我有一个标签和一个按钮。单击按钮时,将调用控制器中的 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
<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>
推荐阅读
- angular - Angular-cli 和 Ckeditor4 自定义构建
- python - 将原始硬盘映像从 S3 存储桶转换为 EC2 映像
- c# - 打开 OpenFileDialog 以更改 PropertyGrid 控件中的属性值?
- arrays - 打字稿参数 - 对象的通用数组和对象键的数组(部分)
- c# - C# Linq 过滤器和求和数据表
- python - 从元素列表中删除元素列表
- docker - nomad 的工作如何从本地 docker 图像中获取
- node.js - 使用 WSL 在 VSCode 中使用 nodemon + babel-node 进行调试
- javascript - 登录后需要相互等待的多个 axios 调用,然后将用户重定向到 Vue.js 上的仪表板 [时间问题]
- javascript - 在 onsubmit 函数完成之前如何防止表单提交?