【问题标题】:Asp.net mvc Url.Action to redirect controller with nullable parametersAsp.net mvc Url.Action 以可空参数重定向控制器
【发布时间】:2015-09-08 16:36:37
【问题描述】:

我有这样的控制器:

public ActionResult Load(Guid? id)
{
   ...
}

当像/Report/Load 这样加载视图时,它会加载页面。

然后我单击页面上的链接以加载一项/Report/Load/7628EDFB-EFD5-E111-810C-00FFB73098ED 并正常加载页面。

现在我想使用这个 url.action 再次重定向到 /Report/Load

<a href="@Url.Action("Load", "Report")">Close Report</a>

但它不断将我重定向到/Report/Load/7628EDFB-EFD5-E111-810C-00FFB73098ED

我需要在 URL.Action 上做什么才能将我重定向到带有 id 的页面?

试过了,没有成功:

<a href="@Url.Action("Load", "Report", null, null)">Close Report</a>

谢谢

【问题讨论】:

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


    【解决方案1】:

    用途:

    <a href="@Url.Action("Load", "Report", new { id = null })">Close Report</a>
    

    查看answer了解更多详情。

    【讨论】:

      【解决方案2】:

      我通过 johnnyRose 和 Beyers 的回答解决了这个问题。

      结果是

      <a href="@Url.Action("Load", "Report", new { id = "" }, null)">Close Report</a>
      

      非常感谢。

      【讨论】:

        【解决方案3】:

        我似乎找不到您的代码的问题。但是,您不能在尝试加载特定项目时简单地在模型中设置 URL。 在控制器内部的加载函数中执行以下操作:

        var urlBuilder =
        new System.UriBuilder(Request.Url.AbsoluteUri)
            {
                Path = Url.Action("Load", "Report"),
                Query = null,
            };
        
           Uri uri = urlBuilder.Uri;
          string url = urlBuilder.ToString();
          YourModel.URL = url;
        

        在您的 view.cshtml 中执行类似

        的操作
          <a href="@Model.URL " target="_blank">reports</a>
        

        【讨论】:

          【解决方案4】:

          你应该在你的 vs 的智能感知中查找-

          因为它接受参数 - ActionName,ControllerName,RouteValues

          其中routeValuesobject 类型的第三个参数,它接受路由值,即new{id=9,name="foobar"}

          方法应该是这样的-

          [HttpGet]
          Public ActionResult Get(int id, string name)
          {
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-12-25
            • 2012-05-31
            • 1970-01-01
            • 2016-03-02
            • 1970-01-01
            • 2010-12-05
            • 2014-10-21
            • 2016-10-06
            相关资源
            最近更新 更多