MVC怎么回事观点与参数 [英] MVC how to return a view with a parameter
问题描述
目前,我有工作的方法,请点击这里的链接code在剃刀在工作时:
@ Html.ActionLink(新用户,注册,注册,新的OpenID {= Model.OpenID})
我想有同样的效果,但与返回从控制器查看,此刻我使用这个code,但没有成功。
返回查看(注册,LM);
我是pretty在MVC新的,所以我有点糊涂了。该视图返回与我的最后code小姐SMT和我支持与部分连接新{OpenID的= Model.OpenID}
你能指出我出了正确的方向?
这是怎么回事我控制器的方法:
公众的ActionResult寄存器(串的OpenID)
尽量避免 ViewData的
和 ViewBag
。尝试使用强类型的ViewModels。这使得你的code干净(和下一个开发谁是要去维护您的code,HAPPY)
有的OpenID
中称为属性的视图模型
公共类RegisterViewModel
{
//其他属性也
公共字符串的OpenID {集;获取;}
}
现在你可以返回视图时,在你的操作设置这个值
法
公众的ActionResult寄存器(串的OpenID)
{
VAR VM =新RegisterViewModel();
vm.OpenID =的OpenID;
返回查看(VM);
}
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)
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);
}
这篇关于MVC怎么回事观点与参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!