Get started with Asp.net MVC

Model

namespace ModelBinding.Models
{
    public class Student {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Sex { get; set; }
 
        public override string ToString() {
            return $"\{\{ id={Id}, name={Name}, age={Age}, sex={Sex ?? "-"} \}\}";
        }
    }
}

View

@section scripts {
    <script>
        function create_student1() {
            $.post('/Student/Create1', { id: 1, name: '张三', age: 11}, function(data) {
                console.log(data)
            })
        }
 
        function create_student2() {
            $.post('/Student/Create2', { id: 2, name: '李四', age: 22}, function(data) {
                console.log(data)
            })
        }
 
        function create_student3() {
            $.post('/Student/Create3', { id: 3, name: '王五', age: 33}, function(data) {
                console.log(data)
            })
        }
    </script>
}

<hr>
<div>
    <a class='btn btn-default' onclick="create_student1()">Demo - Create Student 1</a>
    <br><br>
    <a class='btn btn-default' onclick="create_student2()">Demo - Create Student 2</a>
    <br><br>
    <a class='btn btn-default' onclick="create_student3()">Demo - Create Student 3</a>
</div>

Controller

public class StudentController : Controller {
    public IActionResult Index() {
        return View();
    }
 
    [HttpPost]
    public async Task<IActionResult> Create1() {
        var model = new Student();
        if (await this.TryUpdateModelAsync(model)) {
            var msg = $"You are creating a student {model.ToString()}...";
            return Json(msg);
        }
        else {
            return Json("Failed to create student.");
        }
    }
 
    [HttpPost]
    public IActionResult Create2([Bind("Id", "Name")] Student model) {
        if (ModelState.IsValid) {
            var msg = $"You are creating a student {model.ToString()}...";
            return Json(msg);
        }
        else {
            return Json("Failed to create student.");
        }
    }
 
    [HttpPost]
    public IActionResult Create3(int id, string name, int age, string sex) {
        var model = new Student() {
            Id = id,
            Name = name,
            Age = age,
            Sex = sex
        };
        var msg = $"You are creating a student {model.ToString()}...";
        return Json(msg);
    }
}

Output

You are creating a student { id=1, name=张三, age=11, sex=- }...
Index:74 You are creating a student { id=2, name=李四, age=0, sex=- }...
Index:80 You are creating a student { id=3, name=王五, age=33, sex=- }...