如何设置的Razor视图@ model.attribute? [英] How to set @model.attribute in razor view?
问题描述
我有一个必填字段,字符串属性{获得;集}中的一类,并要设置它在剃刀值。有点像下面的可能?
@ model.attribute =什么'
首先,资本化的问题。
@model
(小写字母M)是剃刀意见保留关键字在你的视图的顶部,例如申报模型类型:
@model MyNamespace.Models.MyModel
后来的文件,你可以参考你想要的属性与 @ Model.Attribute
(大写M)。
@model
声明模型。 模式
引用模型的实例。
其次,您可以分配一个值,你的模型,后来在页面中使用它,但是当页面提交您的控制器动作,除非它是在一个表单字段中的值不会是持久的。为了在模型绑定过程中得到值回您的模型,您需要将值分配给一个表单字段,例如:
选项1
在你的控制器动作,你需要为你的页面的第一个视图模型,否则当您尝试设置 Model.Attribute
的模型
对象将是空的。
控制器:
//此接受[HTTPGET]在默认情况下,所以它会被用来渲染页面的第一个电话
公众的ActionResult SomeAction()
{
为MyModel模型=新为MyModel();
//可选:如果你想在你这里视图,而不是集的属性,您可以
// model.Attribute =无所谓;
返回查看(模型);
}[HttpPost] //此操作接受发送到服务器的数据
公众的ActionResult SomeAction(为MyModel模型)
{
// model.Attribute现在将无所谓
返回查看(模型);
}
查看:
@ {Model.Attribute =无所谓;} @ *只有做到这一点这里如果你不这样做在控制器 @
@ Html.HiddenFor(M => m.Attribute); @ *这将让这个属性=什么的时候,页面提交到控制器* @
选项2
或者,由于模型是基于域名的,则可以跳过创建在您的控制器的型号和仅举表单字段的名称相同模型属性。在这种情况下,一个名为属性的隐藏字段设置为任何,将确保在页面提交,值无所谓将获得绑定到模型的属性
属性在模型绑定过程。请注意,它不一定是一个隐藏字段,只用任何HTML输入字段 NAME =属性
。
控制器:
公众的ActionResult SomeAction()
{
返回查看();
}[HttpPost] //此操作接受发送到服务器的数据
公众的ActionResult SomeAction(为MyModel模型)
{
// model.Attribute现在将无所谓
返回查看(模型);
}
查看:
@ Html.Hidden(属性,无所谓);
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'
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");
这篇关于如何设置的Razor视图@ model.attribute?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!