Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set @model.attribute in razor view?

I have a required field, string attribute{get; set} in a class and want to set it's value in razor. Is something like the following possible?

@model.attribute = "whatever' 
like image 566
genxgeek Avatar asked Sep 26 '11 23:09

genxgeek


People also ask

How do you use a model in Razor view?

Right-click in the Store Index action method and select Add View as before, select Genre as the Model class, and press the Add button. This tells the Razor view engine that it will be working with a model object that can hold several Genre objects.

How do you specify a model in view?

To declare the model type, use the @model directive. Show activity on this post. Note the lowercase @model since uppercase prints the value of the Model property.

HOW include model in Cshtml?

cshtml file we need to right click on GetEmployeeInfo() Action Method & Click on Add View. From the Add View Window, give the View Name as GetEmployeeInfo, Template as Create, and Model class as EmployeeInfo. cs and click on Add, with this GetEmployeeInfo. cshtml file will be added in Views Folder.

How do I declare a variable in Razor page?

To declare a variable in the View using Razor syntax, we need to first create a code block by using @{ and } and then we can use the same syntax we use in the C#. In the above code, notice that we have created the Code block and then start writing C# syntax to declare and assign the variables.


1 Answers

First, capitalization matters.

@model (lowercase "m") is a reserved keyword in Razor views to declare the model type at the top of your view, e.g.:

@model MyNamespace.Models.MyModel

Later in the file, you can reference the attribute you want with @Model.Attribute (uppercase "M").

@model declares the model. Model references the instantiation of the model.

Second, you can assign a value to your model and use it later in the page, but it won't be persistent when the page submits to your controller action unless it's a value in a form field. In order to get the value back in your model during the model binding process, you need to assign the value to a form field, e.g.:

Option 1

In your controller action you need to create a model for the first view of your page, otherwise when you try to set Model.Attribute, the Model object will be null.

Controller:

// This accepts [HttpGet] by default, so it will be used to render the first call to the page public ActionResult SomeAction() {     MyModel model = new MyModel();     // optional: if you want to set the property here instead of in your view, you can     // model.Attribute = "whatever";     return View(model); }  [HttpPost] // This action accepts data posted to the server public ActionResult SomeAction(MyModel model) {     // model.Attribute will now be "whatever"     return View(model); } 

View:

@{Model.Attribute = "whatever";} @* Only do this here if you did NOT do it in the controller *@ @Html.HiddenFor(m => m.Attribute); @* This will make it so that Attribute = "whatever" when the page submits to the controller *@ 

Option 2

Or, since models are name-based, you can skip creating the model in your controller and just name a form field the same name as your model property. In this case, setting a hidden field named "Attribute" to "whatever" will ensure that when the page submits, the value "whatever" will get bound to your model's Attribute property during the model-binding process. Note that it doesn't have to be a hidden field, just any HTML input field with name="Attribute".

Controller:

public ActionResult SomeAction() {      return View(); }  [HttpPost] // This action accepts data posted to the server public ActionResult SomeAction(MyModel model) {     // model.Attribute will now be "whatever"     return View(model); } 

View:

@Html.Hidden("Attribute", "whatever");

like image 84
nekno Avatar answered Sep 30 '22 06:09

nekno