How to send Json object (or string data) from Javascript xmlhttprequest to MVC Controller

后端 未结 3 1958
别跟我提以往
别跟我提以往 2020-12-16 13:28

I created a web application in ASP.NET MVC and trying to call a controller through Javascript AJAX. In Jquery we can send a json object which MVC Model Binder automatically

3条回答
  •  太阳男子
    2020-12-16 14:00

    Here's an example. It assumes that you are using ASP.NET MVC 3.0 which has a built-in JsonValueProviderFactory. If this is not your case you could take a look at this blog post.

    View model:

    public class MyViewModel
    {
        public string Prop1 { get; set; }
        public string Prop2 { get; set; }
    }
    

    Controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    
        public ActionResult SomeAction(MyViewModel model)
        {
            return Content("success", "text/plain");
        }
    }
    

    View:

    
    

提交回复
热议问题