将复杂对象从视图传递到控制器:一个对象始终为空

发布于 2024-09-30 16:00:59 字数 1103 浏览 0 评论 0原文

我将一个复杂对象作为 Model 传递给 View 作为

 alt text

但当我从视图中获取模型时,一个特定对象始终为 null 而其他复杂类型通常通过

alt text

我的视图是默认的 编辑 强类型视图

我错过了什么

ModelState 错误说

从类型“System.String”到类型“Julekalender.Database.CalendarInfo”的参数转换失败,因为没有类型转换器可以在这些类型之间进行转换。

为什么我对其他类型没有得到相同的结果? 如何自动转换?


我添加了 3 个字段(因为 T4 模板不附加此类型),但在 POSTing 时我仍然得到 null

下面绿色框内的是字段

<div class="editor-field">
    <%: Html.TextBoxFor(model => model.Calendar.Guid)%>
</div>

alt text


即使将操作重命名为

[HttpPost]
public ActionResult General2(GeneralInfo model)

也会出现相同的错误

alt text

I'm passing a complex object as a Model to the View as

alt text

but when I get the Model back from the View, one particular object comes always null while other complex types are normally passed through

alt text

my View is the default Edit Strongly Typed View

alt text

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 POSTing

The green boxed below is the field

<div class="editor-field">
    <%: Html.TextBoxFor(model => model.Calendar.Guid)%>
</div>

alt text


Even renaming the Action to

[HttpPost]
public ActionResult General2(GeneralInfo model)

gives the same error

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨与橙与城 2024-10-07 16:00:59

确保当您使用此向导时,在视图中为日历对象的每个属性生成输入字段,以便在您发布表单时将它们发送到控制器操作。我不确定情况是否如此(尚未验证向导是否对复杂对象执行此操作,我从未使用过此向导)。

在生成的 HTML 中,您应该具有:

<input type="text" name="Calendar.Prop1" value="prop1 value" />
<input type="text" name="Calendar.Prop2" value="prop2 value" />
... and so on for each property you expect to get back in the post action
... of course those could be hidden fields if you don't want them to be editable

更新:

问题来自于您的操作方法中有一个名为 calendar 的字符串变量和一个具有名为 Calendar 属性的对象> 这令人困惑。尝试重命名它:

[HttpPost]
public ActionResult General2(string calendarModel, GeneralInfo model)

也不要忘记在您的视图中重命名它。

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:

<input type="text" name="Calendar.Prop1" value="prop1 value" />
<input type="text" name="Calendar.Prop2" value="prop2 value" />
... and so on for each property you expect to get back in the post action
... of course those could be hidden fields if you don't want them to be editable

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 called Calendar which is confusing. Try renaming it:

[HttpPost]
public ActionResult General2(string calendarModel, GeneralInfo model)

Also don't forget to rename it in your view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文