0

I'm trying to create a one view where I'll could edit data from 2 different model. I read this manual: Multiple models in a view and some other topics on SO, and I successful got data from 2 models in my view, but I can't understand why I can't edit it in my view.

So, in my view I have smth like:

@model Tuple<GroupProjectsModel,InfrastructureModel>

@Html.EditorFor(m => m.Item1.Data)

@Html.EditorFor(m => m.Item2.Data)

Then, when I'm trying to save data, some mistakes happened. I have following code in controller:

[HttpPost]
    [InitializeEditPageAttribute]
    public ActionResult Edit(GroupProjectsModel get, InfrastructureModel getInf)
    {
        if (ModelState.IsValid)
        {

             ....

            return ReturnView(get.Id, NameModule);
        }
        var tuple = new Tuple<GroupProjectsModel, InfrastructureModel>(get, getInf);
        return View(tuple);
    }

In this case variable ModelState.IsValid is equal false anyway. But I'm trying another case:

        [HttpPost]
    [InitializeEditPageAttribute]
    public ActionResult Edit(Tuple<GroupProjectsModel, InfrastructureModel> tupleFromModel)
    {
        if (ModelState.IsValid)
        {
           ...

            return ReturnView(tupleFromModel.Item1.Id, NameModule);
        }
        var tuple = new Tuple<GroupProjectsModel, InfrastructureModel>(tupleFromModel.Item1, tupleFromModel.Item2);
        return View(tuple);
    }

and got mistake too. I have no idea what to do.

Community
  • 1
  • 1
coffee1054
  • 11
  • 2
  • 3
    How can you possibly need more than one model for your view when your *models are designed specifically for a view in the first place* (and if they aren't, they should be)? – Ant P Apr 07 '14 at 14:31
  • 1
    You should create a model that has both models inside it. This is just creating problems for yourself – Michael Coxon Apr 07 '14 at 14:36
  • 1
    Is there a reason why you aren't just using partial views? – EkoostikMartin Apr 07 '14 at 14:38
  • How partialViews can help me here? I can create partial view, but anyway, how I'll save data in 2 models? – coffee1054 Apr 07 '14 at 14:47
  • If you're trying to save data for two models at once, the solution is to merge the two models into one. I can give you an example if that will help. – Pete Apr 07 '14 at 15:48

1 Answers1

0

Just created another class that holds both classes.

public class EditViewModel
{
   public GroupProjectsModel groupProjectsModel {get; set;}
   public InfrastructureModel infrastructureModel {get; set;}
}
CinnamonBun
  • 1,150
  • 14
  • 28