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 ?

12
  • 1
    can you show your View? Commented Aug 26, 2016 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) Commented Aug 26, 2016 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. Commented Aug 26, 2016 at 13:34
  • Do you have multiple forms on your View ? Commented Aug 26, 2016 at 13:35
  • 1
    Ok can you please share your View OR .cshtml page where you want to Implement the same . Commented Aug 26, 2016 at 13:52

1 Answer 1

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

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

9 Comments

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.
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 Tell me how are you doing loop without knowing the lenght of the object ?
That's easy @DaniilT. for (int i = 0; i < Model.Service.Count(); i++) { //do stuff for each service }
Again.. heres an duplicate question to yours stackoverflow.com/questions/8894442/…
|

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.