MVC2:无法将 String 转换为 ExtensionDataObject (不知道我想这样做)

发布于 2024-08-16 23:06:41 字数 920 浏览 6 评论 0原文

我在 ASP.Net MVC2 页面上收到以下 InvalidOperationException:

The parameter conversion from type 'System.String' to type 'System.Runtime.Serialization.ExtensionDataObject' failed because no type converter can convert between these types.

In a Post 操作,但我真的不确定它指的是什么。我正在使用数据注释验证:

public class FamilyPersonMetadata
{
    [Required(ErrorMessage = "Name Required")]
    public String Name;

    [Required(ErrorMessage = "Date of Birth required")]
    [DateTime(ErrorMessage = "Invalid Date")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d")]
    public DateTime DateOfBirth;
}

[MetadataType(typeof(FamilyPersonMetadata))]
public partial class FamilyPerson
{
}

我的视图继承自具有 FamilyPerson 子类型的 ViewPage。我只是创建名称与 FamilyPerson 匹配的控件,然后提交表单,但由于某种原因,我的 ModelState 无效,上面的错误显然就是原因。我对错误的性质感到非常困惑。类似的代码适用于其他视图和操作。

有人能给我指出可能导致这种情况的事情的方向吗?

I'm getting the following InvalidOperationException:

The parameter conversion from type 'System.String' to type 'System.Runtime.Serialization.ExtensionDataObject' failed because no type converter can convert between these types.

In a Post action on my ASP.Net MVC2 page, but I'm really not sure what it's referring to. I'm using data annotation validation:

public class FamilyPersonMetadata
{
    [Required(ErrorMessage = "Name Required")]
    public String Name;

    [Required(ErrorMessage = "Date of Birth required")]
    [DateTime(ErrorMessage = "Invalid Date")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d")]
    public DateTime DateOfBirth;
}

[MetadataType(typeof(FamilyPersonMetadata))]
public partial class FamilyPerson
{
}

And my view inhertis from a ViewPage with a subtype of FamilyPerson. I just create controls with names matching those of FamilyPerson and then submit the form, but for some reason my ModelState is invalid and the above error is apparently the reason. I'm quite perplexed as to the nature of the error. Similar code is working for other views and actions.

Could someone point me in the direction of things to look at that might cause this?

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

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

发布评论

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

评论(4

山人契 2024-08-23 23:06:41

关于 [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d")]

"{0:d" 应为 "{0:d}"

Regarding [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d")]

"{0:d" should be "{0:d}"

三生池水覆流年 2024-08-23 23:06:41

这是问题的实际解释以及如何解决它: http://www.shawson.co.uk/codeblog/mvc-strongly-typed-view-returns-a-null-model-on- post-back/comment-page-1/

简短摘要:不要为视图参数指定与模型字段相同的名称,除非它们代表相同的名称值(并且具有相同的类型)。

Here's an actual explanation of the problem, and how to solve it: http://www.shawson.co.uk/codeblog/mvc-strongly-typed-view-returns-a-null-model-on-post-back/comment-page-1/

Short summary: Don't give your view parameters the same names as model fields, unless they represent the same value (and have the same type).

草莓味的萝莉 2024-08-23 23:06:41

它似乎已经自行消失了。诡异的。

It seems to have gone away on its own. Weird.

轮廓§ 2024-08-23 23:06:41

这可能对某人有帮助:

我抛出了这个异常,因为我认为有多种形式。

然而,其中一种表单没有显式设置“action”属性。也就是说,我使用的是这个构造函数:

@using (Html.BeginForm())

而不是这个:

@using (Html.BeginForm("ACTION_METHOD", "CONTROLLER", FormMethod.Post, null))

这将导致表单中发布的参数不正确。具体来说,业务模型对象被包含在表单中,而它不应该包含在表单中。反过来,.Net 随后尝试将 System.String 转换为业务模型对象,而此时它不应该尝试进行此类转换。

解决方案是使用稍后重载的方法,并确保在回发时为表单设置正确的“action”属性。

仅供参考:要检查表单的“action”属性,请使用 FireBug 并检查 HTML,找到 Form 元素,“action”属性将在那里,其中包含提交表单时将回传给服务器的所有参数。

This may help someone:

I had this exception being thrown because i had mulitiple forms in my view.

However one of the forms did not explicitly set the 'action' attribute. That is, i was using this constructor:

@using (Html.BeginForm())

Instead of this one:

@using (Html.BeginForm("ACTION_METHOD", "CONTROLLER", FormMethod.Post, null))

This would result in the incorrect parameters being posted with the Form. Spoecifically, the business model object was being included on the Form when it shouldn't be. In turn, .Net was then trying to convert a System.String to a business model object when it shouldn't be attempting such a conversion.

The solution is to use the later overloaded method and ensure that the correct 'action' attribute is being set for your Form upon postback.

FYI: To inspect the 'action' attribute of your Form, use FireBug and inspect the HTML, find the Form element and the 'action' attribute will be there with all the parameters that will be posted back to the serber when that Form is submitted.

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