0

我正在从数据库中读取枚举值,然后将其绑定到模型。当我使用 ajax 发布表单时,以某种方式枚举未绑定或模型属性为 null 或零,但它在视图上正确显示。我在下面发布了代码。我使用实体框架和 mvc3

//model code constructor
public CarModel(Car car)
{
State=(CarState)car.State;
//car.State comes in as an int
//etc setting other variables
}

//CarState property
public CarState {get;set;}

//model code
@Html.DisplayFor(m=>m.CarState)

//Controller code()
Save(CarModel car)
{
//I have code that saves the changes
}

我到达“汽车”的那一刻,CarState 没有任何价值。

4

1 回答 1

1

目前尚不清楚您如何将此值传递给控制器​​操作。您只显示了一些@Html.DisplayFor(m=>m.CarState),但显然这只在视图中显示了一个标签。它不会将任何内容发送回服务器。如果您想发回一些值,则必须在表单中使用输入字段。

例如:

@Html.EditorFor(m => m.CarState)

HiddenFor或者如果您不希望用户编辑它,请使用字段。

In any case you need to send that value to the server if you expect the model binder to be able to retrieve it. The model binder is not a magician. He cannot invent values. He binds values to your model from the Request.

于 2012-06-20T09:20:49.747 回答