I am currently struggling with posting a JSON object to an ASP.NET MVC controller without using Ajax. I have tried various methods but have not been successful so far. Here is what I have attempted:
- Backend Parameter Object
public class Person
{
public string Name { get; set; }
public string Email { get; set; }
}
- Backend Controller
public class BookingController : Controller
{
[HttpPost]
public ActionResult AddPerson(Person person)
{
return View();
}
- Frontend Javascript & Html
<form action="/booking/addperson" method="post">
<input type="hidden" name="person" id="person" />
<button type="submit" class="btn btn-sm btn-primary">Submit</button>
<script>
var person = { "Name": "ML", "Email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c2afae82afeca1adaf">[email protected]</a>" }
$("#person").val(JSON.stringify(person));
</script>
</form>
Unfortunately, the parameter in the controller was null when I submitted the form on step 3. However, I could see the actual data in the Request object inside the controller (refer to screenshot).
https://i.sstatic.net/5tM1d.png
Can anyone provide insights on how to successfully bind the JSON object to the controller parameter when posting a form? Any help is greatly appreciated! Thanks!