将复杂对象从视图传递到控制器:一个对象始终为空
我将一个复杂对象作为 Model
传递给 View
作为
但当我从视图中获取模型时,一个特定对象始终为 null
而其他复杂类型通常通过
我的视图是默认的 编辑
强类型视图
我错过了什么?
ModelState 错误说
从类型“System.String”到类型“Julekalender.Database.CalendarInfo”的参数转换失败,因为没有类型转换器可以在这些类型之间进行转换。
为什么我对其他类型没有得到相同的结果? 如何自动转换?
我添加了 3 个字段(因为 T4 模板不附加此类型),但在 POST
ing 时我仍然得到 null
下面绿色框内的是字段
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Calendar.Guid)%>
</div>
即使将操作重命名为
[HttpPost]
public ActionResult General2(GeneralInfo model)
也会出现相同的错误
I'm passing a complex object as a Model
to the View
as
but when I get the Model back from the View, one particular object comes always null
while other complex types are normally passed through
my View is the default Edit
Strongly Typed View
What am I missing?
The ModelState Error says
The parameter conversion from type 'System.String' to type 'Julekalender.Database.CalendarInfo' failed because no type converter can convert between these types.
Why don't I get the same for the other types? How is it automatically converted?
I have added 3 fields (as the T4 template does not append this types) but I still get null when POST
ing
The green boxed below is the field
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Calendar.Guid)%>
</div>
Even renaming the Action to
[HttpPost]
public ActionResult General2(GeneralInfo model)
gives the same error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保当您使用此向导时,在视图中为日历对象的每个属性生成输入字段,以便在您发布表单时将它们发送到控制器操作。我不确定情况是否如此(尚未验证向导是否对复杂对象执行此操作,我从未使用过此向导)。
在生成的 HTML 中,您应该具有:
更新:
问题来自于您的操作方法中有一个名为
calendar
的字符串变量和一个具有名为Calendar
属性的对象> 这令人困惑。尝试重命名它:也不要忘记在您的视图中重命名它。
Make sure that when you use this wizard there are input fields generated in the view for each property of the Calendar object so that when you post the form they will be sent to the controller action. I am not sure this is the case (haven't verified if the wizard does it for complex objects, I've never used this wizard).
In the resulting HTML you should have:
UPDATE:
The problem comes from the fact that you have a string variable called
calendar
in your action method and an object which has a property calledCalendar
which is confusing. Try renaming it:Also don't forget to rename it in your view.