0

I have a form that has key value array like this:

enter image description here

In this array, keys has not started from zero so when I defined Request model for get this array as you can see I can't get any value from this array.

Notice that when I try to send this array from zero key that's work correctly:

enter image description here

But I need to send this array with that approach because the keys make sense.

Request Model Class:

public class Correction
{
    [Required]
    public int theory_exam_id { get; set; }

    [Required]
    public int[] answers { get; set; }
}

Controller Method:

[HttpPost("correction")]
public async Task<ActionResult<TheoryExam>> CorrectionTheoryExam([FromForm] Correction correction)
{
    return Ok(correction);
}

2
  • 1
    I'm not sure that it applies to Asp net core (never tried), bur you can check this post for non sequential indices -> haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx Commented Jul 17, 2020 at 12:53
  • is there any way to implement this issue in other approach? i'm beginner in asp so I thankful for helping Commented Jul 17, 2020 at 12:59

1 Answer 1

3

Try this approach

answers.Index = 50
answers[50] = 1
answers.Index = 61
answers[61] = 2
answers.Index = 82
answers[82] = 3

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

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.