0

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.

4
  • 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'). Commented Jul 15, 2014 at 10:44
  • @pawel ok, so how can I take this jquery array and pass it on to the controller along with the other model fields ("MyInt" in my case)? Commented Jul 15, 2014 at 10:48
  • Something along these lines: <input type="hidden" name="MyInt" value="<%= MyInt %>"> <input type="hidden" name="ComplexObject" value="" />, then set the value of ComplexObject input to JSON.stringify( jQueryArray ) before submitting the form. Commented Jul 15, 2014 at 10:56
  • @pawel - I was hoping for another solution, but ended up with yours. please post an answer so I can accept. Thx! Commented Jul 15, 2014 at 16:13

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Simple but good, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.