1

I faced a problem. I can't figure out how to post collection with dependency. I'll show you a code then it maybe clearer what I'm trying to achieve.

I have ViewModel:

public class ProjectViewModel : BaseViewModel
{
    public int Id { get; set; }
    public string Customer { get; set; }
    public string CustomerRepresentative { get; set; }
    public string ProjectTitle { get; set; }
    public string WWSNumber { get; set; }
    public List<Los> Service { get; set; }
}

LosViewModel:

public class Los
{
    public int LosNumber { get; set; }
    public string Service { get; set; }
    public List<Pos> Pos { get; set; }
}

Pos view model:

public class Pos
{
    public int PosNumber { get; set; }
    public string PosDescription { get; set; }
}

As you see first ViewModel has list. That meens that when user fills LosNumber and enters Service he can add to it PosNumber and PosDescription.

I can't find information how to post with Razor such model. All I have found is how to display such ModelViewm but not edit.

Maybe somebody had faced this kind of problem ? Any help ?

Daniil T.
  • 1,145
  • 2
  • 13
  • 33
  • 1
    can you show your View? – teo van kot Aug 26 '16 at 13:32
  • It looks your problem is bigger than a complex post. This isn't really a complex model, MVC will know how to serialize your posted data. Your post method just needs to have the model type as the receiving parameter. public ActionResult MyPostMethod(ProjectViewModel model) – pmeyer Aug 26 '16 at 13:33
  • Not sure if that's the issue you are having or if you dont know how to perform the post at the view level. – pmeyer Aug 26 '16 at 13:34
  • Do you have multiple forms on your View ? – Sunil Kumar Aug 26 '16 at 13:35
  • @teovankot I'm trying to create one. And I can't figure out how. – Daniil T. Aug 26 '16 at 13:50
  • @pmeyer I know that. But I'm trying to figure out how to post when you Have Object wich have List in it and that list also have list in it. I mean how to create N inputs for every of those lists. – Daniil T. Aug 26 '16 at 13:50
  • @SunilKumar no, I have only one. – Daniil T. Aug 26 '16 at 13:51
  • 1
    Ok can you please share your `View` OR `.cshtml` page where you want to Implement the same . – Sunil Kumar Aug 26 '16 at 13:52
  • @SunilKumar Sorry, I meant that I don't have one yet – Daniil T. Aug 26 '16 at 13:59
  • Ok I think you have to use multiple for inserting multiple los values on a form. Are you using jQuery into your project ? – Sunil Kumar Aug 26 '16 at 14:06
  • You don't need multiple forms, you can use Partials Views to create the view, and pass each item of the list to the partial and it will render the controls. Or you can do a for loop... – pmeyer Aug 26 '16 at 14:09
  • Check this out http://stackoverflow.com/a/8896840/1706578 – pmeyer Aug 26 '16 at 14:12

1 Answers1

2

You need to put the collection into a for loop in your view, then reference the object by index on the collection. MVC is smart enough to figure out that this is a collection when you post it back. Something like...TextBoxFor(m => Model.Service[i].LosNumber). The collection inside a collection will most likely just be a nested for loop.

MVC Razor view nested foreach's model

Community
  • 1
  • 1
Grace
  • 2,548
  • 5
  • 26
  • 23
  • It the approach when you Lenght of the object that you want to display is known. But in my case I can't tell how much elements there will be in List. Maybe I should ask how to dynamically create EditorFor boxes with Indexes. – Daniil T. Aug 26 '16 at 14:03
  • you can do a for loop inside razor on the list and that way you can get the index == i to generate the controls. – pmeyer Aug 26 '16 at 14:10
  • @pmeyer Tell me how are you doing loop without knowing the lenght of the object ? – Daniil T. Aug 26 '16 at 14:20
  • That's easy @DaniilT. for (int i = 0; i < Model.Service.Count(); i++) { //do stuff for each service } – pmeyer Aug 26 '16 at 14:51
  • Again.. heres an duplicate question to yours http://stackoverflow.com/questions/8894442/mvc-razor-view-nested-foreachs-model/8896840#8896840 – pmeyer Aug 26 '16 at 14:54
  • @pmeyer Let's figure out this. When you sending Model to View often it's empty and after form post it is populated. Now.... if I use Model.Service.Count(); it will return me 0. Because on creational stage there is no data in Model. – Daniil T. Aug 27 '16 at 18:31
  • @DaniilT So you are saying that would like to dynamically create these new items in your UI so insert new records. Although this can be done with Razor, Id suggest you start looking at a jquery framework to easily do this stuff in the UI. For example, Knockout.js is a simple and easy to use framework. – pmeyer Aug 29 '16 at 13:46
  • @pmeyer I already did. I decide to use two seperate form's. One is to create Project and another one - Ajax Form to create Los and Pos for it. – Daniil T. Aug 30 '16 at 01:09
  • @pmeyer you mentioned that this is possible with razor, right? Can you provide example? – Daniil T. Sep 15 '16 at 00:21