0

I have the following in my Api Controller:

[AcceptVerbs("POST")]
public Model.ViewModel.ContactSaveRequest DeleteMethod(Model.ViewModel.ContactSaveRequest methodsToDelete)
{
    var contactMethodRepos = new Model.ContactMethodRepository();

    foreach (var contactMethod in methodsToDelete)
    {
        contactMethodRepos.Delete(contactMethod);
        return contactMethod;
    }
}

This is my class defining a contact method

[JsonProperty("id")]
public int ID { get; set; }

[JsonProperty("contactID")]
public int ContactID { get; set; }

[JsonProperty("typeOfContactMethodID")]
public int TypeOfContactMethodID { get; set; }

[JsonProperty("text")]
public string Text { get; set; }

[JsonProperty("methodsToDelete")]
public IEnumerable<ContactMethod> methodsToDelete { get; set; }

ContactSaverequest class:

public class ContactSaveRequest
{
    [JsonProperty("contact")]
    public Contact Contact { get; set; }

    [JsonProperty("contactMethods")]
    public IEnumerable<ContactMethod> ContactMethods { get; set; }        
}

I have an array which pushes methods into it to be deleted (methodsToDelete). I am trying to use the Delete method on the array but keep getting the issue that contactSaveRequest doesn't contain a definition for GetEnumerator.

Matthew Walton
  • 9,809
  • 3
  • 27
  • 36
996jc
  • 23
  • 1
  • 2
  • 4

2 Answers2

2

It sounds like you just want to use:

foreach (var contactMethod in methodsToDelete.ContactMethods)

You can't iterate over a ContactSaveRequest, but you can iterate over the IEnumerable<ContactMethod> that is returned by the ContactMethods property.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @moleman: "that doesn't seem to work" doesn't give us any useful information. What goes wrong when you try that? – Jon Skeet May 02 '14 at 10:40
  • Sorry, it says that 'the name contact does not exist in the current context'. – 996jc May 02 '14 at 10:44
  • @moleman: Oh, right - fixed now. I would have hoped you could have worked out that typo though, if you'd understood the answer. (It's always more important to understand the answer than to just plug in the code and hope.) – Jon Skeet May 02 '14 at 10:46
-1

Try to implement IEnumerable interface like this

public class ContactSaveRequest : IEnumerable {

}

Siraj Hussain
  • 874
  • 9
  • 25