首页 > 解决方案 > 如何获得“” 从视图到控制器的“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);
    })
});

请帮忙。

标签: javascriptc#jqueryasp.netasp.net-mvc

解决方案


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

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

推荐阅读