0

I have the following code in my view .cshtml

...
<td class="Centrado">
<input class="plato" value="" id="TComida">                                                                                
</td>
....

and i want to set a value that comes from the controller, the value is from the class Comida the property Price,

....
@model Util.Comida
Util.Comida menu = new Util.Comida();
menu= (Util.Comida)ViewData["Comida"];
....

¿What can i do to set the value menu.Price to my input class="plato" value="" id="TComida" without losing the css styles aplied thx to my class="plato"?

I have checked that the object menu is correctly populated with data from the controller. Sorry for my english and thx in advance.

4

1 回答 1

4

这不是你应该在视图中做的事情:

@model Util.Comida
Util.Comida menu = new Util.Comida();
menu= (Util.Comida)ViewData["Comida"];

如果模型是 aUtil.Comida那么控制器应该向视图提供一个实例。例如,在控制器中返回视图时:

var model = new Util.Comida();
// set properties, invoke logic, etc.
return View(model);

然后在视图中,模型固有地存在于Model属性中。所以使用它的一个值,你可以引用那个属性。例如:

<input class="plato" value="@Model.Price" id="TComida">

或者甚至使用 HTML 助手来发出input标签,这可以带来更多的框架功能。像这样的东西:

@Html.TextBoxFor(m => m.Price, new { id = "TComida", @class = "plato" })

关键是控制器为视图提供模型,视图不创建模型或调用其上的任何逻辑。视图中的代码通常应仅限于绑定到模型上的属性。实际逻辑进入模型,控制器调用该逻辑并将结果模型状态提供给视图。

于 2014-10-17T15:33:23.627 回答