3

I'm pulling my hair out on this one.

I am trying to implement a multi-step wizard, and i'm using the Html.Serialize html helper in MVC3 Futures. This works well, except one of the properties in my model is a SelectList. I don't want this property serialized (and it blows up when it tries anyways).

I can't use [NonSerialized] because that only works on fields, not properties. I've even tried some of the other normal ways such as [XmlIgnore] (which I didn't think would work anyways).

Can anyone suggest an attribute that will ignore a property in a model when using Html.Serialize?

EDIT:

The error I get when I try to serialize is a InvalidDataContractException. There is this message:

Type 'System.Web.Mvc.SelectList' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.

However, if I do this then I have to mark all the members with [DataMember] just to exclude 1 property, which seems kind of stupid.

UPDATE:

A quick example of this is this bit of code (make sure to add reference to System.Runtime.Serialization.dll):

Test.cs

[Serializable]
public class Test
{
    public int ID { get; set; }
    [IgnoreDataMember]
    public SelectList TestList { get; set; }
}

HomeController.cs

public ActionResult About()
{
    return View(new Test() { ID = 0, TestList = new SelectList(new [] {""})});
}

Home/About.cshtml

@using Microsoft.Web.Mvc
@model MvcApplication3.Models.Test 

@Html.Serialize("Test", Model)

This generates the InvalidDataContractException

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291

1 Answers1

4
public class MyViewModel
{
    [IgnoreDataMember]
    public SelectList Items { get; set; }

    ...
}

or simply:

public class MyViewModel
{
    public IEnumerable<SelectListItem> Items { get; set; }

    ...
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • [IgnoreDataMember] doesn't do anything either. I still get the same Serialization error, probably because there's no [DataContract] on the class (that's my guess). However, Adding DataContract means adding [DataMember] attributes, so I'm not sure what the purpose is. – Erik Funkenbusch Feb 15 '12 at 04:54
  • Are you saying that the serializer ignores IEnumerables by default? – Erik Funkenbusch Feb 15 '12 at 04:55
  • @MystereMan, the problem is with the `SelectList` class itself. It cannot be serialized. When you use `IEnumerable`, the property is not ignored but it will serialize if the concrete type is for example `SelectListItem[]` or `List`. I have tested the `IgnoreDataMember` attribute and it worked for me with the `SelectList` type. Could you show a narrowed down version of your view model allowing to reproduce the problem? – Darin Dimitrov Feb 15 '12 at 06:32
  • 1
    @MystereMan, remove the `[Serializable]` attribute from your Test class. Look at my answer. There is no such attribute on MyViewModel. – Darin Dimitrov Feb 15 '12 at 17:37
  • Doh! I had assumed you had left out the attribute. I was under the impression that the [Serializable] attribute was required. – Erik Funkenbusch Feb 15 '12 at 17:47
  • If I leave off the [Serializable] attibute, I get: "Assembly 'x' is not marked as serializable" when I try to serialize it. – Talon Mar 13 '13 at 06:33