简体   繁体   中英

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

I have controller like this:

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

When load the view like /Report/Load it load the page.

Then I click on link on page to load one item /Report/Load/7628EDFB-EFD5-E111-810C-00FFB73098ED and it loads the page fine.

Now I want to redirect again to /Report/Load using this url.action

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

But it keeps redirecting me to /Report/Load/7628EDFB-EFD5-E111-810C-00FFB73098ED

What I need to do on URL.Action to redirect me to page with the id?

Tried without success:

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

Thanks

Use:

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

See this answer for more details.

I got it fixed by following answer from johnnyRose and Beyers.

The result is

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

Thanks a lot.

I can't seem to find issue with your code. But Can't you simply set the URL in the model when trying to load the specific item. In the load function inside the controller do something like this:

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;

In your view.cshtml do something like

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

You should have looked up in your vs's intellisense-

As it accepts arguments- ActionName , ControllerName , RouteValues

在此处输入图片说明

Where routeValues is the third argument that is object type and it accepts route values ie new{id=9,name="foobar"}

Where method would be like-

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM