V
V
VanilaSpirit2020-10-14 15:47:18
ASP.NET
VanilaSpirit, 2020-10-14 15:47:18

C# ASP.NET. Why is the model not being passed?

I form an object in js:

var modelTitle = "пример";           
var obj = {
                    Name: "lalala",
                    Age: 3,
                    Count: 4
                };


I do a POST
$.post("@Url.Action("Create", "User")",
                {
                    title: modelTitle,
                    model: JSON.stringify(obj)
                });

I accept in the controller:
public void Create(string title, User model)
        {

        }


But the model itself does not reach the controller (model = null, although the title does). The Name/Age/Count fields are the same whatever is there.

What else can you try?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
cicatrix, 2020-10-14
@cicatrix

Only a STRING is passed through the web request.
public void Create(string title, User model)
What is the User type? Does ASP.Net know how to deserialize the JSON you pass into the User type?
You are passing a string (JSON.stringify), but you want to get some kind of object. Change to:

public void Create(string title, <b>string</b>model)

A string with JSON will arrive in the model, parse it already inside the controller and create the desired object.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question