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)
The default behavior of the View method ( return View(); ) is to return a view with the same name as the action method from which it's called. For example, the About ActionResult method name of the controller is used to search for a view file named About.
Redirection is very easy, you just call controller and then action in that as above suggested. There is option available to pass parameter too. return RedirectToAction("Tests", new { ID = model.ID, projectName = model. ProjectName });
A controller action might return a view. However, a controller action might perform some other type of action such as redirecting you to another controller action.
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);
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With