Reputation: 1896
I have a form that I would like the users to submit to the server. The (simplified) model looks like that:
public class MyData
{
public int MyInt { get; set; }
public ICollection<ComplexObject> MyArray { get; set; }
}
public class ComplexObject
{
public int MyComplexObjectInt { get; set; }
public string MyComplexObjectString { get; set; }
}
The controller action that receives this object looks like this:
[HttpPost]
public ActionResult Create(MyData model)
{
...
}
I have a "ComplexObject" array in the client side (jQuery) that I populate with user's input.
The problem: How can I set the "MyArray" value to contain the jQuery array values and retrieve this array in the controller?
Note: I've googled this issue quite a bit and all the solutions I've found talked about ajax. I'm not interesting in these solutions and I wonder if it can be done without ajax.
Thanks.
Upvotes: 0
Views: 564
Reputation: 36965
If something can be done with AJAX it can be done using a standard POST or GET request just as well. If your endpoint is process.aspx you just need to use <form action="process.aspx">
instead of $.ajax('process.aspx')
.
So if you need to get some data from your backend, combine it with user-provided data then process it on the server with a full round-trip (not AJAX) you can use a form and hidden inputs:
<form action="process.aspx" method="POST" id="myForm">
<input type="hidden" name="MyInt" value="<%= MyInt %>" />
<input type="hidden" name="ComplexObject" value="" />
</form>
Then when you need to send the ComplexObject
along with MyInt
, you can serialize that object as JSON string, put it into the hidden field and submit the form:
$('[name="ComplexObject"]').val( JSON.stringify( userObject ) );
$('#myform').submit();
Upvotes: 1