C# MVC如何返回带有参数的视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11476263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:00:00  来源:igfitidea点击:

MVC how to return a view with a parameter

c#asp.net-mvcrazor

提问by GibboK

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

目前我有一个有效的方法,当单击 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 }

我对 MVC 很陌生,所以我有点困惑。返回的视图与我的最后一个代码miss smt 和我支持的部分有关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)

采纳答案by Shyju

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

尽量避免ViewDataViewBag。尝试使用强类型ViewModels。这使您的代码干净(以及下一个将维护您的代码的开发人员,HAPPY

Have a Property called OpenIDin your ViewModel

有一个属性调用OpenID你的ViewModel

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

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

现在您可以在返回视图时在您的action方法中设置此值:

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

回答by Jerry

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

您可以将任何您想要的数据添加到 ViewBag 变量中。

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

MVC3 剃刀视图

@ViewBag.OpenID

回答by Andre Calil

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

请看一下这个视图(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 =)

我们只是通过 get(即查询字符串)传递值。通过匹配名称,MVC 能够为我们做魔术 =)

Hope this helps.

希望这可以帮助。

Regards

问候