I am a JSON object in a form which allows users to add options. I want to post this JSON to a ASP.NET MVC controller as a hidden field WITHOUT Ajax. I researched a couple of posts and tried to do this, but without luck. What I have done so far is:
- 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": "[email protected]" }
$("#person").val(JSON.stringify(person));
</script>
</form>
Unfortunately, when I submitted the form on step 3, the parameter in the controller was null, but I could find the actually data in the Request object inside the controller. (see screenshot below)
Does anyone know what do I miss to actually bind the JSON object to the controller parameter when posting a form? Thanks!
