3869

MVC 4 Action Result er

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.

Recommend

  • Query to delete all rows in a table hibernate
  • MVC3 Dynamic Return URL
  • Use Html.DropDownListFor to get a selected value
  • Changing dbContext at runtime
  • Mysql group concat on double join
  • How to remove columns from CDC (in SQL Server)
  • Asp.Net MVC3 adding search functionality
  • How to Add new Row Dynamically in ASP.Net MVC 5
  • Reload MVC2 user control with jQuery
  • How does ASP.NET get line numbers in it's generic error handler
  • The ViewData item that has the key 'MaritalStatus' is of type 'System.String' bu
  • Declare ViewBag on controller
  • The model item passed into the dictionary is of type A, but this dictionary requires a model item of
  • Model binder ValueProvider appends to the existing value + MVC 4
  • Recursion in ASP.NET Core Razor views
  • MVC extension method error
  • Activation Function choice for Neural network
  • Creating a DropDownList
  • How to getText() from the input field of an angularjs Application
  • Combining two different ActiveRecord collections into one
  • Angularjs pass function from Controller to Directive (or call controller function from directive) -
  • Django invalid literal for int() with base 10
  • Eloquent update method change created_at timestamp
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • Insert into database using onclick function
  • Deselecting radio buttons while keeping the View Model in synch
  • Deleting and Updating values from a cusrsor adapter
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Cannot Parse HTML Data Using Android / JSOUP
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Getting Messege Twice Using IMvxMessenger
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • How to load view controller without button in storyboard?