Home > Net >  Angular Asp.Net MVC. Where is the "data"?
Angular Asp.Net MVC. Where is the "data"?

Time:01-28

Angular method:

addSubject() {
    const data = {
        name: this.subject.name, 
        description: this.subject.description, 
        teachersCount: this.subject.teachersCount,
        type: this.subject.type
    }
        
    this.http.post("https://localhost:7042/Subject/addsubject", data).subscribe(res => console.log(res.toString()))
}

url spelled correctly.

Asp.Net MVC - How to get the data?

public string AddSubject()
{
        var data = "How to get the 'Data'?";
        return "OK";
}

How to get the data in Asp Net MVC method?

I tried:

var data = Request.Form["data"]
var data = Request.Query["data"]
public string AddSubject(object data)
{
        return "OK";
}

CodePudding user response:

You can grab the data via parameters or wrap them in an object. For example with parameters:

public string AddSubject(string name, string description, int teachersCount, string type)

Or alternatively you can use a class to do this:

public string AddSubject(SubjectData data)

You can then create a class like this:

public class SubjectData 
{
    string Name { get; set; }
    string Description { get; set; }
    int TeachersCount { get; set; }
    string Type { get; set; }
}

The values will be bound to that object by the framework.

  • Related