【问题标题】:How to pass parameter in @url.Action如何在@url.Action 中传递参数
【发布时间】:2018-05-18 05:07:19
【问题描述】:

我找到了这段代码

<button type="button" class="btn btn-inverse btn-danger"
    onclick="@("window.location.href='" + @Url.Action("ActionName", "ControllerName") +"'");">
Link
</button> 

用于重定向-工作正常, 但现在我需要将参数传递给 this 。 请帮忙。 参考以下How do I redirect a user when a button is clicked?

【问题讨论】:

标签: jquery html asp.net-mvc razor


【解决方案1】:

@Sribin,下面是你可以使用的基础,

您的控制器代码将是,

 // GET: home
    public ActionResult Index()
    {
        User uObject = new User();
        uObject.FirstName = "abc";
        return View(uObject);
    }

    public ActionResult IndexTest(string id)
    {
        return View();
    }

你的cshtml代码将是

@model WebApplication2.Models.User
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
<br />
<tr>
    <input type="button" onclick="SaveLookup()" value="submit" />

    <button type="button" class="btn btn-inverse btn-danger"
            onclick="@("window.location.href='" + @Url.Action("IndexTest", "Home", new {id = Model.FirstName}) +"'");">
        Link
    </button> 
</tr>

您也可以从视图包或任何其他临时数据中获取值,而不是使用模型值!

【讨论】:

  • 谢谢,这就是我要找的东西
  • 这是 MVC 非常基础的基础。最初的学习步骤可能会发生,他们将其视为重复查询、常见且不合逻辑的问题。如果您处于学习阶段,请不要担心您的提问是对的:)
【解决方案2】:

@Sribin,试试下面,

<button type="button" class="btn btn-inverse btn-danger"
        onclick="@("window.location.href='" + @Url.Action("IndexTest", "Home", new {id = "ID"}) +"'");">
    Link
</button> 

【讨论】:

  • 感谢您的帮助。而不是“ID”如何传递动态值?
  • @Sribin 你可以通过控制器发送ID
猜你喜欢
  • 2018-04-04
  • 2015-06-18
  • 2018-04-04
  • 1970-01-01
  • 2013-07-18
  • 1970-01-01
  • 1970-01-01
  • 2015-08-01
  • 1970-01-01
相关资源
最近更新 更多