8

At the moment I have a Method that work, it is working when clicking a link here the code in Razor:

@Html.ActionLink("New User ,Register", "Register", new { OpenID = Model.OpenID })

I would like have the same effect with but returning the View from the Controller, at the moment I'm using this code with no success

return View("Register", lm);

I'm pretty new at MVC so I'm a bit confused. The view returned with my last code miss smt and I support is connected with the part new { OpenID = Model.OpenID }

Could you point me out in the right direction?

This how it is the method for my controller:

public ActionResult Register(string OpenID)
tereško
  • 58,060
  • 25
  • 98
  • 150
GibboK
  • 71,848
  • 143
  • 435
  • 658
  • I'm sorry, I couldn't understand what you're trying to do. Do you want to link a different view and pass some data to it? – Andre Calil Jul 13 '12 at 18:34
  • I need call a different View and passing some data that OpenID = Model.OpenID as it would be when a user click the link – GibboK Jul 13 '12 at 18:36

4 Answers4

17

Try to avoid ViewData and ViewBag . try to use strongly typed ViewModels. That makes your code clean (and the next developer who is gonna maintain your code, HAPPY)

Have a Property called OpenID in your ViewModel

public class RegisterViewModel
{
     //Other Properties also
     public string OpenID { set; get; }
}

Now you can set this value when returning the view, in your action method:

public ActionResult Register(string OpenId)
{
     var vm = new RegisterViewModel();
     vm.OpenID = OpenId;
     return View(vm);
}
Community
  • 1
  • 1
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • Thanks for your comment in my specific case the Action Method to be called as this signature public ActionResult Register(string OpenID) could you post an example of code for me? thanks – GibboK Jul 13 '12 at 18:50
  • I need smt like return ActionResult("Register", lm.OpenID); but in my case does not work any idea? – GibboK Jul 13 '12 at 18:53
8

You can add any data you want to a ViewBag variable.

In your controller you'd set the value as such.

Controller

public ActionResult Register()
{
    ViewBag.OpenID = OpenID;

    return View()
}

And in your razor view you can access it the same way

MVC3 Razor View

@ViewBag.OpenID
Jerry
  • 1,775
  • 4
  • 22
  • 40
4

Please, take a look at this view (ViewA):

<div>
    @Html.ActionLink("My link", "ViewB", new { someData = "some data I'm passing on" })
</div>

And these two actions:

    public ActionResult ViewA()
    {
        return View();
    }

    public ActionResult ViewB(string someData)
    {
        //Here someData has the value "some data I'm passing on"
        return View();
    }

We're just passing the values by get (that is, the query string). By matching the names, MVC is able to do the magic for us =)

Hope this helps.

Regards

Andre Calil
  • 7,652
  • 34
  • 41
0

I currently use this form:

return View("Name View", new { parameterName = Value})

Practical example:

return View("Register", new { OpenId = 1})