MVC 4动作结果ER [英] MVC 4 Action Result er
问题描述
我Asp.net MVC 4是新
I am new in Asp.net MVC 4
我写的使用功能MVC控制器
I write function using MVC controller
public ActionResultRole_name(string role)
{
return role;
}
我的问题是如何显示在CSHTML的Razor视图返回值?
My question is how to display return value on cshtml razor view?
推荐答案
您可以返回的ViewResult:
You could return a ViewResult:
public ActionResult Role_name(string role)
{
return View((object)role);
}
和相应 Role_name.cshtml
查看您可以使用此值:
and in the corresponding Role_name.cshtml
view you could use this value:
@model string
<div>The role is @Model</div>
注意它是必要的铸角色
变量设置为对象
,以确保正确的显示方法重载(拍摄模式)正在被解决。
Notice how it was necessary to cast the role
variable to an object
to ensure that the proper View method overload (taking a model) is being resolved.
在另一方面,你也可以使用这始终是最好的做法视图模型:
On the other hand you could have used a view model which is always best practice:
public class MyViewModel
{
public string Role { get; set; }
}
,你的控制器动作可能填充并传递给相应的stringly类型的视图:
which your controller action could have populated and passed to the corresponding stringly typed view:
public ActionResult Role_name(string role)
{
var model = new MyViewModel();
model.Role = role;
return View(model);
}
和视图:
@model MyViewModel
<div>The role is @Model.Role</div>
此外,我建议你考虑看看一些在 asp.net/mvc
网站。
这篇关于MVC 4动作结果ER的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!