簡體   English   中英

Asp.net MVC Url.Action使用可空參數重定向控制器

[英]Asp.net mvc Url.Action to redirect controller with nullable parameters

我有這樣的控制器:

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執行什么操作才能將我重定向到帶有ID的頁面?

嘗試不成功:

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

謝謝

采用:

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

有關更多詳細信息,請參見此答案

我通過遵循johnnyRose和Beyers的回答來解決此問題。

結果是

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

非常感謝。

我似乎找不到您的代碼的問題。 但是,您是否不能在嘗試加載特定項目時簡單地在模型中設置URL。 在控制器內部的load函數中,執行以下操作:

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>

您應該已經看過vs的智識-

由於它接受arguments- ActionNameControllerNameRouteValues

在此處輸入圖片說明

其中routeValuesobject類型的第三個參數,它接受路由值,即new{id=9,name="foobar"}

哪里的方法會像-

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM