【问题标题】:Calling ActionResult from PartialView never hits从 PartialView 调用 ActionResult 永远不会命中
【发布时间】:2022-08-03 01:00:00
【问题描述】:

非常新的 MVC 并通过了一些教程。做过以下事情:

添加一个名为 CustomerController 的控制器。 添加2个方法

    public ActionResult Render()
    {
        // Go to a third party WebAPI and get the results in a List
       return PartialView(\"CustomerList\", custList);
    }

    public ActionResult SomeTest()
    {
        Response.Redirect(\"Somepage\");
    }

然后我添加一个页面 (LandingView.cshtml) 并创建一个名为 CustomerListPartialView 并将以下代码添加到 LandingView 页面

@Html.Action(\"Render\", \"Customer\")

当我查看此页面时,它会呈现带有客户列表的页面。 PartialView 的 HTML 是

@using (Html.BeginForm(\"SomeTest\", \"Customer\"))
{

    <div class=\"container\">
        @foreach (var i in Model)
        {
            <a href=\"@i.Url\">
                <div class=\"product-grid__item__name\">@i.Title</div><br />
                <div class=\"product-grid__item__price\">@i.Price.ToString(\"C\")</div>
            </a>
             
            <input type=\"button\" id=\"btnGo\" value=\"Go\" />
        }
    </div>
}

当我单击按钮时,它永远不会点击 SomeTest 方法?在调试模式下,我在RenderSomeTest 上设置了一个断点,在页面加载时渲染命中,但是当单击Go 时它从未命中SomeTest 方法?

我在这里想念什么?

    标签: c# asp.net-mvc razor razor-pages


    【解决方案1】:

    将输入元素的“类型”属性值设置为“提交”而不是“按钮”。这将触发点击时的表单提交。

    <input type="submit" id="btnGo" value="Go" />
    

    您可能会遇到一些构建错误,因为 SomeTest() 控制器方法需要 ActionResult 类型的返回值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-28
      • 2021-04-15
      • 2014-11-01
      • 2016-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多