【问题标题】:Sending data with ActionLink GET call使用 ActionLink GET 调用发送数据
【发布时间】:2012-12-09 21:11:11
【问题描述】:

我应该如何在不使用 RouteData.Values["id"]; 的情况下做到这一点;

我正在视图中使用此操作调用

@Html.ActionLink("Post","Create","Post", new {id=item.Id }, null)

这就是行动

    // GET: /Post/Create
    public ActionResult Create()
    {
        var x = (string)RouteData.Values["id"];
        var model = new Post() { DateCreated = DateTime.Now, BlogID = int.Parse(x)};

        return View(model);
    } 

有更好的方法吗?

【问题讨论】:

    标签: asp.net html.actionlink


    【解决方案1】:

    嗯,通常的做法是:

    public ActionResult Create(string id)
    {
        int intID;
    
        if (int.TryParse(id, out intID)) {
            //...   
        }
    
        //...
    }
    

    【讨论】:

    • 不客气。您可以根据需要添加任意数量的参数。使用 ASP.NET MVC,您还有另一个优势。 url 末尾的任何值都会传递给 id 参数,例如 "/Post/Create/23"。祝你有美好的一天。
    【解决方案2】:
    @Html.ActionLink("Post","Create","Post")
    

    别忘了,你也可以写 HTML:<a href="/Post/Create">Post</a>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-28
      • 2019-05-29
      • 2011-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多