0

I have a model like this

public class Extended_Model
    {
        public IEnumerable<Model1> Models1 { get; set; }
        public IEnumerable<Model2> Models2{ get; set; }
        public IEnumerable<Model3> Models3 { get; set; }
        public IEnumerable<Model1_Model2> Rel_Model1_Model2 { get; set; }
        public IEnumerable<Model1_Model3> Rel_Model1_Model2 { get; set; }
    }

I would like to ask you how Html.RenderAction works. I have a Create view for the Extended_Model with 3 RenderActions:

@Html.RenderAction("Create", "Model1");

@Html.RenderAction("Create", "Model2");

@Html.RenderAction("Create", "Model3");

So they show me the Create view of each model.

What I want is to fill the info of the models (hiding their submit buttons), create them and create the relations.

How can I do it? Is RenderAction what I need or anything else?

Thanks!!!

Calum
  • 1,889
  • 2
  • 18
  • 36
dak
  • 199
  • 1
  • 5
  • 18

1 Answers1

0

I would use EditorTemplates for this. This is actually a perfect scenario for using them.

Basically, you would set them up like this:

\Views\Shared\EditorTemplates\Model1.cshtml

@model Model1
<!--Whatever HTML code you want to edit/create your Model1-->

And repeat for your other models. Then in your parent view, you can easily render these out:

@model Extended_Model
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.EditorFor(m => m.Models1)
    @Html.EditorFor(m => m.Models2)
    @Html.EditorFor(m => m.Models3)
    @Html.EditorFor(m => m.Rel_Model1_Model2)
    @Html.EditorFor(m => m.Rel_Model1_Model2)
    <input type="submit" value="Save" />
}

You don't even need to implement looping logic, as MVC, by convention, will loop over your IEnumerables for you.

Community
  • 1
  • 1
Jerad Rose
  • 15,235
  • 18
  • 82
  • 153
  • I've used EditorTemplates some days ago and I had some exceptions I didn't know how to solve. I'll try to use them again. Thank you!!! ;) – dak Mar 26 '14 at 08:53
  • Antiforgerytoken could be the problem if it's used in @model Model1? I've read its used to prevent cross site scripting. – dak Mar 26 '14 at 09:07
  • I've updated it with the form/anti-forgery code. If your controllers are validating anti-forgery (and they should be), you will need to include `@Html.AntiForgeryToken()`. – Jerad Rose Mar 26 '14 at 12:18