且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将模型从视图传递到控制器

更新时间:2023-02-25 18:00:26

您应添加包含您的值的隐藏输入:

You should add hidden inputs containing your values :

@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.Content)

这是因为模型绑定程序将搜索输入(例如文本框或隐藏字段)以获取值并将它们与模型属性相关联(基于输入名称). DisplayFor不会创建任何输入,因此在提交表单时,模型绑定程序找不到您的值.

This is because the model binder will search for inputs (like textboxes, or hidden fields) to get values and associated them with your model properties (based on input names). No input is created with DisplayFor, so the model binder can't find your values when you submit your form.