1

I have a lot of data that needs to be passed from a controller to a view and I am trying to use strongly typed View Models where possible.

Take an example where you have a database of loads of people - We want to edit a person whilst also presenting a list of everyone with the same surname.

public class person
{ 
  public string ID { get; set; }
  public string FirstName { get; set; }
  public string LastName { get; set; }
}

public class testviewmodel
{
 public List<person> people { get; set; }
 public person newperson { get; set; }
}

I can't use testviewmodel as the model for the post because there is a lot more going on in the form/data. I have managed to build a model that contains nearly all the form data, other than the ones from the View Model.

I generate some items in the form via:

<input asp-for="newperson.Firstname" class="form-control"/>

This in return generates:

<input class="form-control" disabled type="text" id="newperson_Firstname" name="newperson_Firstname" value="xxxx" />

However, I have tried adding newperson_Firstnameto my model alongside quite a few other combinations, and, I am just not able to see the data.

Can anyone please assist and let me know what I am doing wrong - or, should I just be adjusting the view model to be more fit for purpose?

...Lastly, is there any equivalent of var_dump($_REQUEST);? At the moment, I'm adding breakpoints and trying to open up different items within Locals, but, it's trial and error and taking ages... I'm just trying to find where the form is!

Mike Brind
  • 28,238
  • 6
  • 56
  • 88
Dev X
  • 89
  • 9
  • You should spend a little time learning how Modelbinding works in MVC Core: https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-2.2 – Mike Brind Jun 19 '19 at 06:26
  • @MikeBrind I won't lie - always have more to learn, but, I feel like I do know the basics of model binding/been doing this a long time... I just can't get viewmodels to work on complex items such as this :( – Dev X Jun 19 '19 at 08:35
  • One thing in addition to my answer below - your tag helper seems to be generating a `disabled` attribute. Values in disabled controls are not included in form posts. – Mike Brind Jun 19 '19 at 08:53

1 Answers1

1

You shouldn't need to dig around in the Request object. If you pass an instance of your ViewModel to your post action, model binding will take care of populating the Person property automatically:

[HttpPost]
public IActionResult Edit(TestviewModel model)
{
    var person = model.Person; // add a breakpoint here, should represent the posted values
}
Mike Brind
  • 28,238
  • 6
  • 56
  • 88
  • What I was trying to get at is that there are many many other properties that are used within the form that don't need to be there as part of the initial view model. I think however I am overthinking this and being stupid, I should be using the same view model for the page and the post, just not populating the fields in the initial load.... I'll experiment and come back, but thank you. – Dev X Jun 19 '19 at 09:02
  • If you want to use Tag helpers to generate inputs for all the properties that you want to have posted back, then those properties should all be in the initial ViewModel. Naturally, they won't be populated. – Mike Brind Jun 19 '19 at 09:22
  • I was an idiot to try to have a different model for posting as to the standard view... I can confirm that by adding the extra fields to the initial view model (and just not using in the initial load), everything worked exactly as expected. – Dev X Jun 22 '19 at 02:41