加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-2 – 将复杂对象从View传递给Controller:一个对象

发布时间:2020-12-16 03:20:01 所属栏目:asp.Net 来源:网络整理
导读:我将一个复杂的对象作为模型传递给View as 但是当我从View中获取Model时,一个特定的对象始终为null,而其他复杂类型通常会通过 我的视图是默认编辑强类型视图 What am I missing ? ModelState错误说 The parameter conversion from type ‘System.String’ to
我将一个复杂的对象作为模型传递给View as

但是当我从View中获取Model时,一个特定的对象始终为null,而其他复杂类型通常会通过

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

What am I missing?

ModelState错误说

The parameter conversion from type ‘System.String’ to type ‘Julekalender.Database.CalendarInfo’ failed because no type converter can convert between these types.

为什么我不能为其他类型获得相同的?它是如何自动转换的?

我添加了3个字段(因为T4模板没有附加此类型)但是在POST时我仍然为null

下面的绿色框是字段

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

甚至将Action重命名为

[HttpPost]
public ActionResult General2(GeneralInfo model)

给出了同样的错误

解决方法

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

在生成的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

更新:

问题来自这样一个事实,即你的action方法中有一个名为calendar的字符串变量,以及一个名为Calendar的属性令人困惑的对象.尝试重命名:

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

另外,请不要忘记在视图中重命名它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读