1

Hey Guys I need help on this, I know I can return List<string> in HTML.BeginForm which looks like this:

@using (Html.BeginForm("Test", "Home", FormMethod.Post, new {@class = "form-horizontal", role = "form"}))
{
<textarea name="logic" style="width: 10em; height: 10em;"></textarea>
<textarea name="logic" style="width: 10em; height: 10em;"></textarea>

<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" class="btn btn-default" value="Register" />
    </div>
</div>
}

I can return List<string> Logic on my Controller HTTPOST

But my question is how will I do it if I need to return a List of Object? Not using a Model because I need to return 2 Objects so I am was thinking like my controller should get it something like this

        public ActionResult Test(List<Model1> model1, List<Model2> model2)

Thanks for those who can help

8
  • Unclear what your asking. What are you models Model1 and Model2? Commented Sep 18, 2015 at 3:12
  • Model1 and Model2 are just examples, let say they have 2 parameters which are string1 and string2 for each Model if it will make it clearer Commented Sep 18, 2015 at 3:15
  • 2
    Then the answer is yes, its easy to do Commented Sep 18, 2015 at 3:17
  • 1
    This site is for helping you with your code. Start with a view model containing 2 collection properties and then pass it to the view. Use a for loop or EditorTemplate's for you model types and post back you view model. Commented Sep 18, 2015 at 3:22
  • 1
    Refer this answer for an example of generating controls in a for loop and this one for using and EditorTemplate Commented Sep 18, 2015 at 4:15

3 Answers 3

4

You can do in provided way

First declare class

 public class TestModel
    {
        public string Name { get; set; }
        public string Text { get; set; } 
    }

Second Controller to accept list of model

 [HttpPost]
        public ActionResult Test(List<TestModel> model1, List<TestModel> model2)
        {
            return View();
        }

Last, View to pass model values

@using (Html.BeginForm("Test", "Home", FormMethod.Post, new {@class = "form-horizontal", role = "form"}))
{
<textarea name="model1[0].Name" style="width: 10em; height: 10em;"></textarea>
<textarea name="model1[0].Text" style="width: 10em; height: 10em;"></textarea>
<textarea name="model1[1].Name" style="width: 10em; height: 10em;"></textarea>
<textarea name="model1[1].Text" style="width: 10em; height: 10em;"></textarea>
<textarea name="model2[0].Name" style="width: 10em; height: 10em;"></textarea>
<textarea name="model2[0].Text" style="width: 10em; height: 10em;"></textarea>
<textarea name="model2[1].Name" style="width: 10em; height: 10em;"></textarea>
<textarea name="model2[1].Text" style="width: 10em; height: 10em;"></textarea>

<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" class="btn btn-default" value="Register" />
    </div>
</div>
}

Put the breakpoint inside Test controller you will see list populated

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

Comments

3

I would suggest to wrap these lists into a view model class like this -

public class ModelView
{
    public List<Model1> model1 { get; set; }
    public List<Model2> model2 { get; set; }
}

...and then the controller will look like -

public ActionResult Test(ModelView modelView)

Within the controller you can easily access the lists.

modelView.model1 
modelView.model2 

2 Comments

Thats a good idea, but what will i put on the name and id of the html inputs? As much i do not like to use editorfor but in your solution i need to use @model to implement it
Bind your View from this ModelView class. Now you can easily have controls for this like - model.<propertyName> OR model.<propertyName>.<subpropertyName> etc for example. Comment from Steplen gives good hint
2

If you want to generate a view with multiple model objects then you need to create a ViewModel comprising properties that are needed from those models. And then reference the view with this ViewModel.

public class Model1
{
    public string prop11 { get; set; }
    public string prop12 { get; set; }
}

public class Model2
{
    public string prop21 { get; set; }
    public string prop22 { get; set; }
}

public class ViewModel
{
    public List<Model1> model1 { get; set; }
    public List<Model2> model2 { get; set; }
}

Then generate the view referencing the viewmodel that will get the properties from both models.

controller action that will be hit from that view:

public ActionResult Test(ModelView modelView) // you can access the viewmodel properties

Comments

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.