71289

I am new in Asp.net MVC 4
I write function using MVC controller
public ActionResultRole_name(string role)
{
return role;
}
My question is how to display return value on cshtml razor view?
Answer1:
You could return a ViewResult:
public ActionResult Role_name(string role)
{
return View((object)role);
}
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; }
}
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);
}
and in the view:
@model MyViewModel
<div>The role is @Model.Role</div>
Also I would recommend you taking a look at some of the getting started tutorials on the asp.net/mvc
site.