I have a model that looks like this:
Business - Branch - Phone(*) - Phone Type - Number - Opening hours (*) - Days in week - Working period (*) - From time - To time - Custom field (*) - Name - Value - Address - Address line - City - State - Zip - Yada yada
I created Editor Templates for each of the class types above.
I want to have a common Business
editor template with a submit form that posts the entire structure to a single action and saves it, both for an existing or new entity.
- Is Editor Templates the right approach? How do I submit the form along its entire downline?
- How do I make Add and Remove buttons to add/remove phone numbers within the form?
- How do I order items in the collection (i.e. I want to have arrows near each phone number so the user can move it up or down in the client list, then handle the saving on server, for that I already have the solution).
Bottom line, my issue is how to get the right values posted back to the server, and how to modify the inner collections on the client. Once the proper data is on the server in this way or another I'll know how to deal with it. My problem is the client side and the correct way of data submission.
Update
I saw this answer, which basically answers the 1st part of my question, tho the latter two still remain (add-remove-order buttons - manage collections on client).
My problem is not how to add/remove/reorder rows at the client's DOM, but how to modify the client data and then receive it in the server in the action the looks like this:
[HttpPost]
public ActionResult Save(Business business)
{
/// blah blah
}
Update
Here is how I try to shove in the new data:
View:
@Ajax.ActionLink("Add", "AddCustomField", new AjaxOptions { UpdateTargetId = "customFields", InsertionMode = InsertionMode.InsertAfter })
Action:
public PartialViewResult AddOpeningTimes()
{
var ot = new OpeningTimes();
ot.WorkingPeriods.Add(new WorkingPeriod());
var e = EditorFor(ot);
//just here for debugging, the values are both empty strings
e.ViewData.TemplateInfo.HtmlFieldPrefix = ViewData.TemplateInfo.HtmlFieldPrefix;
return e;
}
//this method is on the base controller:
protected PartialViewResult EditorFor<TModel>(TModel model)
{
return PartialView("EditorTemplates/" + typeof(TModel).Name, model);
}
The thing is the name
for the appropriate fields are not enumerated as needed (Branches[0].CustomField[0].Key
), instead, it's just Key
.