Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
447 views
in Technique[技术] by (71.8m points)

asp.net mvc 3 - 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'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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");


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...