0
@model  SepiaCMSClassLib.Model.Page
<table>
    @using (Html.BeginForm())
    {
        <tr>
            <td width="300">
                @Html.EditorFor(m=>m.pagedetail)
            </td>
        </tr>
    }
</table>

My model

public class Page
{
     public int PageID { get; set; }
     public int SortId { get; set; }
     public List<PagesDetail> pagedetail { get; set; }
}

public class PagesDetail
{
    public int PageTitle{ get; set; }
    public int PageDescription{ get; set; }
    public int LangId{ get; set; }
}

In my action method i am passing nothing to this view as it is the create view

public ActionResult Create()
{         
    return View();
}

The problem is that my EditorFor() template is loading when i pass data to it it is loading , it is not loading if i pass no data. i understand that is the default behavior. I wanted to use nested model binding ( only passing page model in the controller posts). Html.Partial() will not give the desired result of nested model binding

How can i use EditorFor template approach in create view?

maztt
  • 12,278
  • 21
  • 78
  • 153
  • Please post model so I can help in details. – Amit Sep 18 '15 at 14:43
  • I have posted the model as well – maztt Sep 18 '15 at 14:51
  • Firstly you have invalid html - a `
    ` element cannot be a child of a `` element (you need to swap them - put the table in the form). Are you wanting to have a view where you can dynamically add new `PagesDetail` items? If so, some options [here](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796) and [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308)
    –  Sep 19 '15 at 02:29
  • @ Stephen Muecke ok so i would not be using the above approach for create? – maztt Sep 19 '15 at 10:34
  • @maz3tt, Not if your wanting to dynamically add multiple `PagesDetail` items in the view. The only thing you can do with your current approach is to add a fixed number of default `PagesDetail` items to the collection in the controller before you pass the model to the view. –  Sep 20 '15 at 07:07
  • @maz3tt: I will Suggest provide name and Id unique and self created. – Amit Sep 21 '15 at 01:50
  • one more question . i have been playing around with begincollection. what i find out that it will work in page->pagedetail scenario. but if i want to drill down like this page->pagedetail->pagesubdetail. it will not work?. – maztt Sep 21 '15 at 08:06
  • It will only create automatic binding in in page->pagedetail ? am i right? – maztt Sep 21 '15 at 08:08
  • Once you have more than one level of nesting it becomes extremely complicated when using the `BeginCollectionItem` helper. I have seen article that created another helper based on `BeginCollectionItem` to do this. I'll have a look for it later and let you know if I find it –  Sep 21 '15 at 08:11
  • The article I was referring to is [Editing and binding nested lists with ASP.NET MVC 2](http://www.joe-stevens.com/2011/06/06/editing-and-binding-nested-lists-with-asp-net-mvc-2/) –  Sep 21 '15 at 08:17

0 Answers0