1

I am trying to make a MVC view accessible from either directly going to that View from a menu or by clicking on a link that'll take me to that same view but with a parameter and with that particular links information instead of seeing a general page if I went straight to it.

public ActionResult Roles(Guid applicationId)
    {
        if (applicationId == Guid.Empty)
        {
            return View();
        }

        var application = new ApplicationStore().ReadForId(applicationId);

        return View(application);
    }

I know for optional parameters you I'd do something like Guid? in the parameters but visual studios doesn't like that and I can't do Guid application = null either. Any Ideas?

2 Answers 2

4

As you already mentioned, make the parameter optional.

public ActionResult Roles(Guid? id) {
    if (id == null || id.Value == Guid.Empty) {
        return View();
    }

    var application = new ApplicationStore().ReadForId(id.Value);

    return View(application);
}

This also assumes the default convention-based route

"{controller}/{action}/{id}"

Where the id is optional in the route template.

id = UrlParameter.Optional

For example

routes.MapRoute(
     name: "SomeName",
     url: "{controller}/{action}/{id}",
     defaults: new { controller = "Account", action = "Index", id = UrlParameter.Optional }
 );
Sign up to request clarification or add additional context in comments.

Comments

1

You could potentially just change the Guid parameter to string. Guid.Empty = 00000000-0000-0000-0000-000000000000 which may cause issues when trying to pass in a null value. if you switch it to something like this (but still use a Guid):

public ActionResult Roles(string applicationId)
{
    if (string.IsNullOrEmpty(applicationId))
    {
        return View();
    }

    var application = new ApplicationStore().ReadForId(applicationId);

    return View(application);
}

it may side-step the errors you're encountering.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.