6

So long story short I have a view where Im using

@using (Html.BeginForm("AddFormToOrder", "Form", FormMethod.Post, new { @id = "OrderForm", enctype = "multipart/form-data" }))

which will have a file upload where once I select the files and click an add to order button, the AddFormToorder will fire using the following info

models

public class Orders
{
    public List<Forms> FormList { get; set; }
    public Orders()
    {
        FormList = new List<Forms>();
    }
}
public class Forms
{
    public ICollection<IFormFile> Files { get; set; }
}

and in my controller I am doing this

    public IActionResult AddFormToOrder(Forms form)
    {
        var Orders = JsonConvert.DeserializeObject<Orders>(HttpContext.Session.GetString("Shop_Orders"));
        Orders.Forms.Add(form);
        var serializedModel = JsonConvert.SerializeObject(Orders);
        HttpContext.Session.SetString("Shop_Orders", serializedModel);
        return RedirectToAction("Index", "Form");
    }

This issue is that once that returns and goes to Index, I deserialize again but then get the following error

JsonSerializationException: Could not create an instance of type Microsoft.AspNetCore.Http.IFormFile. Type is an interface or abstract class and cannot be instantiated.

now if i add an order without files in it, everything works perfectly fine and this error never throws. I believe the issue is serializing the model with an IFormFile, but I have no idea how to get around it. Is it possible to serialize IformFile types?

JDawg848
  • 121
  • 2
  • 10
  • could you paste your json here? – Prany Jul 10 '18 at 17:30
  • @Prany Do you mean what the serialized string looks like before deserialization? This: "[{\"ContentDisposition\":\"form-data; name=\\\"Cal_Files\\\"; filename=\\\"Sign.PNG\\\"\",\"ContentType\":\"image/png\",\"Headers\":{\"Content-Disposition\":[\"form-data; name=\\\"Cal_Files\\\"; filename=\\\"Sign.PNG\\\"\"],\"Content-Type\":[\"image/png\"]},\"Length\":57038,\"Name\":\"Cal_Files\",\"FileName\":\"Sign.PNG\"}]" – JDawg848 Jul 10 '18 at 18:01
  • Why not configure the deserializer to use the type information in the json? – user3272686 Jul 10 '18 at 22:47
  • I do not know how to do that good sir/madam, main reason as to why I posted this in the first place haha I'm brand new to json and serialization/deserialization. I've been attempting to use a custom ConcreteConverter to little success – JDawg848 Jul 11 '18 at 13:23
  • @JDawg848 Did you ever figure this out? – C.Ikongo Apr 26 '19 at 16:45

0 Answers0