c# - Binding issue in a list at a complex model to an mvc application -


I'm new. NET MVC web application. I'm using version 4 of web application. I am developing a page to manage the roles of a simple membership provider, and I want to do it dynamically. To clarify what I mean, something like this will happen:

User Inorolls < / P>

Something is really simple to do this, I have a complex maple like that:

  public class UserInRolesModel {public list & lt; String & gt; AllRoleNames {get; Set; } Public listing & lt; UserInRoles & gt; User receive {receive; Set; } Public UserInRolesModel () {AllRoleNames = New list & lt; String & gt; (); UsersInRoles = new list & lt; UserInRoles & gt; (); } Public User Inorolles Modell (List & lt; String & gt; Allroll name, list & amp; UTI; User Inorlix & gt; User Inorolles) {AllRoleNames = allRoleNames; UsersInRoles = usersInRoles; }} Public class UserInRoles {public UserInRoles (string user, list & lt; string & gt; user, IEnumerable & lt; string & gt; allRoleNames} {user = user; Roles = centroll (userroll, allroll name); } Private listing & lt; Bool & gt; Setroll (List & lt; String & gt; User, Inimerable & lt; String & gt; Allroll name) {Return all rollnames. Select (UserR.Content) .Oolist (); } Public string user {get; Set; } Public listing & lt; Bool & gt; Roles (get; set;} Public Zero UpdateRoles (List & lt; String & gt; allRoleNames) {var roleProvider = (SimpleRoleProvider) System.Web.Security.Roles.Provider; Var roles added = new list & lt; string & For (;); (Int i = 0; I and Lt; roles; number; i ++) {if (roles [i]) {roles include (Add all stroke [i]);}} RoleProvider. AddUsersToRoles (new [] {user}, roles added.Tower ());}}  

In my view, I have something that shows roll names To::

   Thread & gt; @ html.DisplayFor (m = & gt; m.AllRoleNames, "AllRoleNames") & lt; / Strong>  

And, I have a DisplayTemplates folder, which contains the AllRoleNames.cshtml file, and here it is that is inside (I Not yet concerned about the layout of the page):

  @ Model List & lt; string & gt; @if (model! = Null) {for (int i = 0; i & Lt; model.count; i ++) {@ Html.HiddenFor (m = & gt; M [i]) @ html.DisplayFor (m = & gt; m [i]}}}  

So far, it's pretty good it shows the correct name, but when I press the Save button If I try to suppress, then MVC should get the appropriate name to bind HTML to the model object in its HTTP post controller action:

  [http post] Public Action Result AdminUsers ( UserInRolesModel model) {// code to save roles goes here View View (model); }  

But I get an empty model (I am just concerned about the list of roles for now, because once I understand what's going on with it, then I can also find out what's going on with the list of my user Inner's object ).

I can find out why MVC is not binding correctly, that's because HTML is something like this:

  & lt; Input ID = "AllRoleNames__0_" name = "AllRoleNames. [0]" Type = "hidden" value = "admin" & gt; Admin & lt; Input ID = "AllRoleNames__1_" name = "AllRoleNames. [1]" Type = "Hidden" value = "Extractor" & gt; Extraterrestrial  

The name trait is being generated in the form of "Olurollam". [0] "When there should be" Allrollum [0] "there is an extra dot, then I got it with a hack proposed by the answer. It can work, but when I tested, I saw that my HTMLFieldPrint was already It was not without the dot, definitely it does not work.

My question is, why MVC produces these names with dots, and to say it in a right way to generate a way I hope that my question is clear, but if not, then I can provide more information. Thanks in advance!

I have found that giving a list as a model is nothing but pain.If you pass that category which includes the list life then it becomes very easy, And the things you expect from them I.e. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Model.mylist [i]) @html DisplayFor (model = & gt; model.MyList [i])}

should produce your property names correctly.


Comments