如何在剃刀视图中设置@model.attribute? [英] How to set @model.attribute in razor view?
问题描述
我有一个必填字段,字符串属性{get;set} 在一个类中,并希望在 razor 中设置它的值.像下面这样的事情可能吗?
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'
推荐答案
首先,大小写很重要.
@model
(小写m")是 Razor 视图中的保留关键字,用于在视图顶部声明模型类型,例如:
@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
在文件的后面,您可以使用@Model.Attribute
(大写M")引用您想要的属性.
Later in the file, you can reference the attribute you want with @Model.Attribute
(uppercase "M").
@model
声明模型.Model
引用模型的实例化.
@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.:
选项 1
在您的控制器操作中,您需要为页面的第一个视图创建一个模型,否则当您尝试设置 Model.Attribute
时,Model
对象将是空.
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.
控制器:
// 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);
}
查看:
@{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 *@
选项 2
或者,由于模型是基于名称的,您可以跳过在控制器中创建模型,而只需将表单字段命名为与模型属性相同的名称.在这种情况下,将名为Attribute"的隐藏字段设置为whatever"将确保当页面提交时,值whatever"将在模型绑定过程中绑定到模型的 Attribute
属性.请注意,它不必是隐藏字段,只需带有 name="Attribute"
的任何 HTML 输入字段即可.
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"
.
控制器:
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);
}
查看:
@Html.Hidden("Attribute", "whatever");
这篇关于如何在剃刀视图中设置@model.attribute?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!