MVC how to return representation with parameter

Right now I have a method that works, it works when I click on the link here the code in Razor:

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

I would like to have the same effect, but return View from Controller, at the moment I am using this code without success

 return View("Register", lm); 

I'm new to MVC, so I'm a bit confused. The view returned with my last omission of smt code, and I keep in touch with the new { OpenID = Model.OpenID }

Could you point me in the right direction?

This is my controller method:

 public ActionResult Register(string OpenID) 
+4
source share
3 answers

Try to avoid ViewData and ViewBag . try using strongly typed ViewModels . This makes your code clean (and the next developer to maintain your code is HAPPY)

You have an OpenID property in ViewModel

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

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

 public ActionResult Register(string OpenId) { var vm = new RegisterViewModel(); vm.OpenID = OpenId; return View(vm); } 
+9
source

You can add any data to the ViewBag variable.

In your controller, you must set the value as such.

controller

 public ActionResult Register() { ViewBag.OpenID = OpenID; return View() } 

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

MVC3 Razor View

 @ViewBag.OpenID 
+4
source

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 simply pass the values ​​through get (i.e. the query string). By picking names, MVC can do the magic for us =)

Hope this helps.

Hi

+4
source

All Articles