0

Description: I have a form for user-friendly input: this is the form

But i can't submit form in this way, coz my model for this form action looks like:

    public string Title { get; set; }  // First input
    public string Description { get; set; }  // Second input
    public string SportsmanId { get; set; }  // Not used
    public List<WorkoutExerciseParam> WorkoutExerciseParams { get; set; }  // Items, combined from list items (show on form screenshot)
    public SelectList AvailableSportsmans { get; set; }  // Dropdown list

So, if I can't submit, I wrote JS code to construct and post consistent model:

$(document)
        .ready(function() {
            $("#submit").click(function() {
                var exerciseList = [];
                /* Assemble exerciseList from OL and UL items */
                var title = $("input#Title").val();
                var description = $("input#Description").val();
                var sportsmanId = $("select#SportsmanId").val();
                $.post('@Url.Action("Create", "Workout")',
                {
                    Title: title,
                    Description: description,
                    SportsmanId: sportsmanId,
                    WorkoutExerciseParams: exerciseList
                });
            });
        });

This code works fine, but I can't redirect after the action is done (like when I just submit the form):code sample

Then, I rewrite JS code so, that it constructs a new hidden form with hidden input and submit it. But I don't know how to create the list of inputs (List from first code sample).

Question:

What is the best practice to submit data to ASP.NET Controller's Action throw JS that I can use RedirectToAction() and View() methods? Do I need construct form (how can I do a list of objects) or how handle RedirectToAction() and View() method in JS?

Arghavan
  • 1,125
  • 1
  • 11
  • 17
murzagurskiy
  • 1,273
  • 1
  • 20
  • 44
  • 1
    Use a normal submit (and what makes you think your model has anything to do with not being able to use a normal submit) –  Jun 02 '17 at 07:06
  • @StephenMuecke coz normal submit doesn't convert UL and OL to List<...> – murzagurskiy Jun 02 '17 at 07:11
  • 2
    Your `
      ` and `
    1. ` are not posted - your form controls are, and you just have to name them correctly to bind to your model. Start by reading the answers [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) and [here](http://stackoverflow.com/questions/40539321/partial-view-passing-a-collection-using-the-html-begincollectionitem-helper/40541892#40541892) (I recommend you use the `BeginCollectionItem()` method
    –  Jun 02 '17 at 07:13
  • @StephenMuecke so, i doen't know about this trick. Thanks. If you draw up your answer, I can mark it as correct and close the question – murzagurskiy Jun 02 '17 at 07:27

1 Answers1

2

You should be making a normal submit rather that ajax if you want to redirect (or be able to return the view and display validation errors if ModelState is invalid. There is no point using ajax, since ajax calls do not redirect.

You have not shown how your dynamically generating the inputs associated with your WorkoutExerciseParam collection, but they just need to be named correctly with indexers so that they will be bound by the DefaultModelBinder. The format needs to be

<input name="WorkoutExerciseParams[0].SomeProperty" .... />
<input name="WorkoutExerciseParams[0].AnotherProperty" .... />
<input name="WorkoutExerciseParams[1].SomeProperty" .... />
<input name="WorkoutExerciseParams[1].AnotherProperty" .... />
....

Your can generate this using javascript, but a better solution which gives you string type binding, client side validation for the dynamically added items and the ability to also delete items is to use the BeginCollectionItem() method as discussed in the answers to