0

I have a Model which contains properties and ICollection of files.

[Required]
public int SerialNo { get; set; }

public Type Type {get; set;}
[Required]
public int TypeId

public virtual ICollection<Files> Files { get; set; }

Now, I am on my Edit Screen, and if Model State is not valid I pass my object again to the view. Like this:

if (!ModelState.IsValid)
{
   return View(MyClass);
}

I am getting System.ArgumentNullException: Value cannot be null. for Files Collection. How can I attach my Files Collection again with my object?

Modified: Use of Collection Files:

@foreach (var item in Model.Files)
{
  <p> @item.Location </p>                  
}

The problem is File is Null.

Ashir K.
  • 121
  • 1
  • 12
  • make sure that is posted back with values, not `null` – Ehsan Sajjad Mar 20 '18 at 08:40
  • How can I do that? Using `Include`? – Ashir K. Mar 20 '18 at 08:46
  • Are your generating form controls for each property of each `Files` in the collection? If not, just call the db to get them again as you did in the GET method –  Mar 20 '18 at 08:46
  • Yes I am generating the form controls. – Ashir K. Mar 20 '18 at 08:50
  • Then they will not be `null` and you will not get that error, so it suggests the code in your view is wrong - but we cant see it :) –  Mar 20 '18 at 08:54
  • Should I post my complete code here? – Ashir K. Mar 20 '18 at 08:57
  • In my `view`, I am looking through the collection and displaying data. But, when `model-state` is not valid and I send back my object the collection is `null`. – Ashir K. Mar 20 '18 at 09:08
  • @AshirK. You need to show how you generate the form controls for the properties in your `Files` collection (your clearly not doing it correctly) –  Mar 20 '18 at 09:19
  • Basically, I am not generating form controls. I am just looping through and showing data. I have updated my question. – Ashir K. Mar 20 '18 at 09:41
  • I got the Problem. I was not sending back to Edit Action. Now I am sending but it's counting as 0, when there are files. – Ashir K. Mar 20 '18 at 10:03
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/167163/discussion-between-stephen-muecke-and-ashir-k). –  Mar 20 '18 at 10:05

0 Answers0