2

I'm trying to dynamically build a table that needs to be bound to a ViewModel on form submission.

First item is the Action the form is submitting to. Then the Parent ViewModel and then the child ViewModel. Below those are two text fields representing the data I need bound.

When I submit the form all the other fields from the page are bound to their respective property but the complex object of ProgramViewModels is not.

What am I missing?

public ActionResult Add(AddEntityViewModel viewModel){
 Code that does something
}

 public class AddEntityViewModel
    {
    public IList<int> Counties { get; set; }
    public IList<ProgramViewModel> Programs { get; set; }
    public IList<int> Regions { get; set; }
    public string EntityName { get; set; }

    public bool IsValid()
    {
        if (string.IsNullOrEmpty(EntityName))
            return false;

        if (Counties == null || Counties.Count == 0)
            return false;

        if (Programs == null || Programs.Count == 0)
            return false;

        if (Regions == null || Regions.Count == 0)
            return false;

        return true;
    }
}

public class ProgramViewModel
{
    public int Id;
    public string SystemId;
}

<input type="hidden" id="Programs[0]__Id" name="Programs[0].Id" data-programid="3" value="3">
<input type="text" id="Programs[0]__SystemId" name="Programs[0].SystemId" style="width:100%" maxlength="50">

Update:

Changed the fields to properties after adiga's answer. But that too did not fix the issue.

public class ProgramViewModel
{
    public int Id { get; set; }
    public string SystemId { get; set; }
}
4
  • Try to initialize the Programs list first? Shouldn't you initialize the list by doing Programs = new List<ProgramViewModel>() in your AddEntityViewModel? Also if you have to grab user's input dynamically from a form, I will just build the object from client side and use jQuery post ($.ajax or $.post) to submit the form with the custom object. MVC is smart enough to bind the custom object to the view model you define. Commented Oct 10, 2017 at 19:24
  • @DavidLiang Building the object via javascript is my next step if I can't get this to work in a timely manner. I was just hoping to use the form submission. Commented Oct 10, 2017 at 20:30
  • 1
    Then show us how your form looks like. The way you wrote Programs[0].Id makes me think you're using @Html.Helper to bind each ProgramViewModel to the form inputs. But then you mentioned in the post that you're trying to dynamically build the table. That's what confused me. If you want to submit dynamic data, you can't use @Html.Helper to bind the input values. It's better to just craft out the JavaScript object yourself on client side that matchs the view model on the Controller action and just do a Post back to server, without using form submit. Commented Oct 10, 2017 at 21:08
  • I am a complete moron. Found the issue. I had a field called Programs and I missed it when I looked over the form for duplicates. Thank you all for you help. Commented Oct 10, 2017 at 21:37

2 Answers 2

2

Your ProgramViewModel contains fields. Change them to properties.

public class ProgramViewModel
{
    public int Id { get; set; }
    public string SystemId { get; set; }
}

The DefaultModelBinder uses reflection and binds only the properties and not fields.

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

7 Comments

Good catch thank you. Unfortunately, that didn't fix the issue. I'll update my original post with the new ProgramViewModel.
@Tyddlywink, after amending that error do you still get null values or a different problem?
@Tyddlywink you're doing something else wrong. Do you get list property null?
@Tyddlywink btw I have updated the question to keep the original post and added an "update" to avoid invalidating the answer and comments
@Tyddlywink that's not possible. With the information provided in the question, this should work. Are you sure the inputs are nested within the form? Are you creating multiple inputs with same name attribute?
|
0

If you have a List of a object, you should be performing a foreach instruction to get all of them:

<% foreach(var x in values) { %>
<div>hello <%= x.name %></div>
<% } %>

1 Comment

The issue is that the Programs property of the AddEntityViewModel is null after form submission. I'm building the table in Javascript as the user enters data.

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.