asp.net mvc - when passing a collection to EditorFor(), it generates invalid names for input elements -
I have a BookCreateModel that contains airline information such as title, publication & amp; Etc. Collection of plus book authors (complex types):
public class BookCreateModel {public string title {get; Set; } Public int year {receives; Set; } Public IList & lt; AuthorEntryModel & gt; Author {get; Set; }} Public class authorentermodel {public string first name {receipt; Set; } Public String LastName {get; Set; }} I used the EditorFor assistant in the Create Code: @ Html.EditorFor (m = & gt; The author selector template is given below: & Lt; Div class = "ptr_authors_wrapper" & gt; For @ (int i = 0; i Author selector template has some wrapper markups, which should be aware of some javascript along with the index of each render item, which Handles the interaction of hair input and needs to be presented once (under the Author Selectionor template), thus it is not possible to get rid of the loop / or author chosen template. The problem editor is now a little strange and the input name is like this:
& Lt; Input id = "Author__0__LlastName" name = "author. [0] .site name" type = "article" value = "" /> As you can see names like authors [0] instead of creating them. First name This adds an extra dot which makes default model typing post unable to parse the data. Any thoughts?
Thank you!
me
@ Html.EditorFor (m = & gt; With m.Authors, "AuthorSelector") :
@ Html.EditorFor (m => m.Authors) And then rename your ~ / views / shared / editorTemplates / AuthorSelector. From Cshtml to ~ / views / shared / editorTemplates / AuthorEntryModel.cshtml and in any one AuthorEntryModel model it is typed strongly and get rid of the loop: @ model authorEntryModel @ Html.TextBoxFor (o = & gt; o.fst_name) @html. TextboxFlow (O = & gt; O.Litinum) ASP Net MVC will automatically present the editor template for all elements of the archive and will generate the appropriate
Update:
My response after seeing your update is:
In your main view:
& lt; Div class = "ptr_authors_wrapper" & gt; @ Html.EditorFor (m = & gt; m.Authors) & lt; / Div & gt; In your editor templates:
@model author Anti Model & lt; Div class = "ptr_author_line" & gt; @html Textboxfor (o = & gt; o.fst_name) @html Textboxfor (o = & gt; o. Lowname) & lt; / Div & gt; You will see the absence of a script in the template which is completely normal. Clippers do not have to do anything in markup, they go to different JavaScript files. In this file you can use jQuery for everything you need to do with your markup. This gives you the method in the form of .index () that you can obtain the index of the element in the matching selector so that you do not need to write any thumbs and your markup Corrupt such as data-line-index features.
Comments
Post a Comment