Click here to Skip to main content
15,918,168 members
Please Sign up or sign in to vote.
1.18/5 (3 votes)
See more:
Hello experts
I passed a list from my javascript file to "PassThings" Action and my list is correctly passed to the controller. But when I want to take it and use "foreach" to give them one by one to the properties of my model, I notice that my "things.WatchingList" is empty and when "Foreach" I encounter Nullable Error(System.NullReferenceException).
here is my Action:
[HttpPost]
public IActionResult PassThings(ThingListViewModel things)
{

    var ThingList = new List<Watching>();

    foreach (var th in things.WatchingList)
    {
        var wtch = new Watching
        {
            Id = th.Id,
            Name = th.Name
        };
        _unitOfWork.watching.Add(wtch);

    }

    _unitOfWork.Save();
    return View();
}

here is my view model:
public class ThingListViewModel
{
    public List<Watching> WatchingList { get; set;}
}

and here is my model:
public class Watching
{
    [Key]
    public string Id { get; set; }
    [Required]
    [MaxLength(30)]
    [DisplayName("Movie Name")]
    public string? Name { get; set; }
}

thank you for your time <3

What I have tried:

i tried using AddRange method but that's not work too,
i changed
foreach (var th in things.WatchingList)
to
foreach (var th in things)
but i got this Error "foreach statement can't operate on variables of type 'ThnigListViewModel' because 'ThnigListViewModel' does not contain a public instance or extension definition for 'GetEnumerator'"
Posted
Comments
Richard Deeming 22-May-24 9:27am    
This seems to be virtually the same as yesterday's question[^], except this time you haven't shown the JavaScript that's making the request or the JSON payload.

As with yesterday's question, the JSON payload needs to match the model that the action is binding to.
Max Speed 2022 22-May-24 19:07pm    
hi, that's i did and worked, this question is different,now my problem is when I want to save the objects from "things" into the database one by one. my foreach have a problem i explain in above <3
Richard Deeming 23-May-24 3:27am    
"I notice that my things.WatchingList is empty"

That's still exactly the same problem as your previous question. And without seeing the JavaScript code or JSON payload, nobody can help you.

So, as you have probably worked out by now. The reason your foreach fails is because you are trying to enumerate the class rather than WatchingList.

Onto the error itself. What would help you to do is put a breakpoint on the first line of the controller and, when you call this method, use the IDE to look at what is present in things. We don't know because we can't see your code running, but you can. Once you know what is present, you can determine what is wrong. My immediate suspicion is that your JavaScript and C# models don't match but, as we can't see things in operation, we can't verify this. Look at the network tab in your browser to see what you have sent across as a request, and match this up with what the controller is seeing. Typically when I've seen issues like this, it has been a casing issue where id is being sent into a property called Id, and the controller doesn't know how to deserialize this.
 
Share this answer
 
Comments
Max Speed 2022 22-May-24 19:08pm    
hi, thank you for your answer, when I make a break point in the first line, yes, I can see the objects that I sent from JavaScript to the controller, my problem is when I want to save the objects from "things" into the database one by one.
Once again, as with your previous question[^]: the model you pass from JavaScript needs to match the model you're using on the server.

Your server model is an object containing a property called WatchingList, which contains a list of Watching objects.

The data you're passing from JavaScript is object containing a property called ThingList, which contains a list of objects.

The two models do not match.
 
Share this answer
 
Comments
Max Speed 2022 23-May-24 6:21am    
thanks alot i find that with your help,really thank you,god bless you <3
The best thing is to convert the list to a data table and insert everything at one instance to sql.

steps to take for the same.

1. Make sure data is available from Js to controller.
2. do foreach and create data table from the list
3. create sp with table as parameter.
4. call the sp from ,c# code and insert all at once using the sp.

This way you don't need to keep the db connection alive for longer time until the foreach completes. you can insert millions of records in a single shot.

refer: Sending a DataTable to a Stored Procedure[^]
 
Share this answer
 
Comments
Richard Deeming 24-May-24 4:39am    
Not relevant to the question that was asked.

The OP was struggling to send data from JavaScript and have it bound to his server-side model. Telling him to convert his server-side model to a completely different type and use a completely different method to process it isn't going to be of any help if he can't get the data into his model in the first place!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900