【问题标题】:Wrong Function Being Called From MVC View从 MVC 视图调用了错误的函数
【发布时间】:2015-08-26 20:05:57
【问题描述】:

我有一个 MVC 视图,它由控制器中的函数启动。该视图有一个按钮,我想用它向同一个控制器中的不同函数提交数据,但它总是返回到启动它的函数。

控制器被调用,ViewForPrepare 视图从 PrepareList 启动,我点击 ViewForPrepare 上的按钮,它再次提交到 PrepareList 而不是 RunList。

在我的控制器中:

public ActionResult PrepareList(int Key)
{

        return "ViewForPrepare";
}

public ActionResult RunList(int Key)
{

        return "OtherView";
}

然后在视图中:

<input type="button" value="Submit Report" id="submit">
<script type="text/javascript">
$(document).ready(function () {
    $('#submit').click(function () { window.location ='@Url.RouteUrl("RunList", new { Key = @Model.caseNumber })' });
});
</script>

所以我按下按钮转到 RunList,但它一直转到 PrepareList。我检查了路由,看起来还可以。我需要做什么才能让按钮提交到 RunList?

【问题讨论】:

  • 确保您的按钮只是一个按钮而不是提交。
  • Ya submit 就是我的名字,它仍然只是一个 type=button

标签: asp.net-mvc asp.net-mvc-4


【解决方案1】:

您现在的代码基本上说“当我单击提交按钮时。将窗口的位置更改为其他位置。”如果这是您想要的,请尝试使用

@Url.Action("RunList", new { Key = Model.caseNumber })

而不是

@Url.RouteUrl("RunList", new { Key = @Model.caseNumber })

并尝试使用&lt;button&gt; 元素而不是&lt;input&gt; 元素。

如果您想要从表单中发布数据,您应该将您的按钮包装在表单标签中(确保将下面的“ControllerName”替换为您的实际控制器。)

@Html.BeginForm("RunList","ControllerName", new { Key = Model.caseNumber })
{
     <input type="submit" value="Submit Report" id="submit">
}

并完全摆脱 javascript,因为在这种情况下没有必要。此外,您必须将您的 RunList 操作标记为 HttpPost 才能正常工作。

[HttpPost]
public ActionResult RunList(int Key)
{
        return "OtherView";
}

【讨论】:

  • 这个解决方案是正确的现在一切似乎都在工作(虽然我从来没有让 [HttpPost] 事情工作,说在控制器中找不到它)。感谢您的帮助。
【解决方案2】:

为什么不直接使用RouteLink 而不是Input

@Html.RouteLink("Submit Report", "RunList", new { Key = Model.caseNumber }, new {@class="btn" })

不确定您使用的是引导程序还是 jquery ui,但有一些 css 类可以使链接看起来像按钮。

ActionLink 的工作方式相同。

@Html.ActionLink("Submit Report", "RunList", "ViewForPrepare ", new { Key = Model.caseNumber }, new { @class = "btn" })

【讨论】:

  • 我之前尝试过这个并得到错误:“从客户端检测到潜在危险的 Request.Path 值 (
  • 您尝试过 Action Link 吗?您的路线与您发布的有关控制器名称和操作名称的任何内容都不匹配。 caseNumber 有很多符号吗?有caseid可以代替吗?
  • 试过 ActionLink 和 RouteLink。 caseNumber 只是一个整数。
【解决方案3】:

使用 VS2015 Pro 我使用 MVC 模板创建了一个项目。

HomeController.cs 添加:

public ActionResult PrepareList(int Key)
{
    return View();
}

public ActionResult RunList(int Key)
{
    return View(); ;
}

Index.cshtml 添加:

@Html.ActionLink("Submit Report", "RunList", new { Key = 4 }, new { @class = "btn" })

在“RunList”中放一个断点,它就起作用了!

使用

    <input type="button" value="Submit Report" id="submit">
@section script{
    <script type="text/javascript">
    $(document).ready(function () {
        $('#submit').click(function () { window.location ='@Url.RouteUrl("RunList", new { Key = 4 })' });
    });
    </script>
}

我收到了消息

A route named 'RunList' could not be found in the route collection.

【讨论】:

  • 你有一个路由文件,其中包含 routes.MapRoute(name: "RunList", url: "Home.mvc/RunList", defaults: new { controller = "PageReports", action = "RunList" });
  • 所以也许你需要改变你的路线,并添加参数Key,看到这个问题stackoverflow.com/questions/17734684/…
【解决方案4】:

当需要发送数据时,必须在该动作中添加属性HttpPost:

[HttpPost]
public ActionResult RunList(int Key)
{
        return "OtherView";
}

【讨论】:

  • 试过了,还是会出错。也试过
  • 嗨,但是,您什么时候想发帖或发帖?因为,如果你有一个表单,你更喜欢使用 post,但我看到你在 javascript 中有一个 window.location,这个调用一个 get,你能解释一下吗?你有更重要的 cde 可以分享吗?
猜你喜欢
  • 2012-12-27
  • 1970-01-01
  • 2014-10-12
  • 1970-01-01
  • 1970-01-01
  • 2017-07-14
  • 1970-01-01
  • 2013-05-30
  • 1970-01-01
相关资源
最近更新 更多