【问题标题】:im trying to call a razor我想打电话给剃刀
【发布时间】:2022-11-23 11:54:07
【问题描述】:

我试过了,但电话打不通,你能帮帮我吗 与帖子相关或解决方案请我 坚持下去,我正在使用 vs 2022

我试试这个,但电话没打通

enter image description here

我想运行这个

    
        public ActionResult GetItems(string name)
        {
            return new JsonResult(name);
            
        }

它位于 /Admin/Rpouters/Router/GetItems

【问题讨论】:

  • 我现在会证明它是正确的...

标签: ajax asp.net-core razor-pages


【解决方案1】:

你需要在你的动作中返回一个List<SelectListItem>类型的数据,你需要将选择的值传递给动作。这是一个演示: HTML:

<select id="IdSelectIdEmpleado" onchange="adddata()">
    <option value="one">1</option>
    <option value="two">2</option>
    <option value="three">3</option>
    <option value="four">4</option>
    <option value="five">5</option>

</select>
<select id="mydrop"></select>

js:

function adddata() {
                $.ajax({
                    type: "POST",
                    data: { name: $("#IdSelectIdEmpleado").val() },
                    url: '/Admin/Rpouters/Router/GetItems',
                    datatype: "json",
                    success: function(data) {
                        for (var i = 0; i < data.length; i++) {
                            $("#mydrop").append("<option value='" + data[i].value + "' selected>" + data[i].text + "</option>");
                        }
                    }
                    })
            }

行动:

[HttpPost("/Admin/Rpouters/Router/GetItems")]
        public ActionResult GetItems(string name)
        {
            return new JsonResult(new List<SelectListItem> { new SelectListItem { Value="1", Text=name+1}, new SelectListItem { Value = "2", Text = name + 2 } });

        }

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    相关资源
    最近更新 更多