Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

[Question] ModelBinding and consecutive integers #5679

Closed
joacar opened this issue Jan 12, 2017 · 7 comments
Closed

[Question] ModelBinding and consecutive integers #5679

joacar opened this issue Jan 12, 2017 · 7 comments

Comments

@joacar
Copy link

joacar commented Jan 12, 2017

Hi,

I generate some models dynamically on the client-side and sends a request the a controller to "inflate" the editor template which is then appended the the DOM.

// Controller
public IActionResult Add(ICollection<Model> models)
{
return PartialView("EditorTemplates/Model.cshtml", models[0]);
}

After appending the resulted template (or one has been deleted) I need to ensure that all models are in consecutive order with respect to name and id html attributes.

Is there any way to manipulate the model binding to accept none-consecutive or perhaps just use another identifier. Perhaps this is more difficult since probbly both model binding and name/id generation is affected.

Thanks!

#5642

@frankabbruzzese
Copy link

Model binder is already able to handle not consecutive indexes. See http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/

@joacar
Copy link
Author

joacar commented Jan 12, 2017

Thanks @frankabbruzzese I will look into that!

@joacar
Copy link
Author

joacar commented Jan 19, 2017

@frankabbruzzese Do you have an example to override the whole process? Ideally I would like to generate <input type="hidden" name="Index" value="@Guid.NewGuid()" /> so that I don't need to iterate all items prior to submission to ensure they are correct. If I add one item at a time they all get indexed with zero.

Perhaps something like

// Signature example (can't recall paramers exactly for existing)
public static IHtmlContent EditorFor<TModel>(..., Func<TModel, string> indexId)
{
 // Generates <input type="hidden" name="Index" value="@indexId()" />
}

// Usage
@for(var i = 0; i < Model.Items.Count; ++i)
{
@Html.EditorFor(_ => Model.Items[i], "Template", model => mode.Id or _ => Guid.NewGuid())
}

@frankabbruzzese
Copy link

You may use Guids! This is the best way to do it. I do the same!
Unlickily I have no simple example, since I use Asp.net coreMvc Control Toolkit that has abstractions to solve this and other problems. You may look at the sources of the grid, but the level of abstraction is very high since everything must work with all ViewModels and also with custom templates(ie partial views) provided by the user. The, code that actuallly do stuffs is contained in providers, so the relevant code for grid is here. and in the various default templates used by the grid.

@joacar
Copy link
Author

joacar commented Jan 20, 2017

Found it rather hard to find the pieces that are related :)

Hopefully I'll have time this weekend to put togheter extensions methods that sit ontop of existingEditorFor{Model} that can provide the user the ability to

  1. Use Guid as identifier (Typically used in scenarions for adding new resources, e.g. contacts to a phone book)
  2. Use unique identifer to TModel (Adding existing resources, e.g. images to a photo album)

@frankabbruzzese
Copy link

frankabbruzzese commented Jan 21, 2017

@joacar, the part of the code dealing with Guids is the ServerCrudController<T, T1, K>, since all grid operations of the server grid are performed through ajax with the help of descendants of this generic controller. Pay attention, there are 2 ways to create a new row one for the batch grid(the one you are interested in) where the guid is packaged like [guid here] and the immediate grid that uses guids in a completely different way.

@Eilon
Copy link
Member

Eilon commented Jun 9, 2017

We are closing this issue because no further action is planned for this issue. If you still have any issues or questions, please log a new issue with any additional details that you have.

@Eilon Eilon closed this as completed Jun 9, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants