However I need to send the list to the controller using ajax, then send the list of models back to to the view, and be able to set the same indexs, in my view, I have the list of models in my controller no problems, but how do I access and set the index
in my view.
Not well understood what you want to do. Namely....
If there were no errors you simply do ModelState.Clear() and repeate the same computations you have done ro render the original page. That is you recompute your indexes as you have done the first time.
Now the problem arise if there were errors.....Normally if you just pass back the model as you received in the view...WITHOUT PERFORMING ModelState.Clear() User will see just its modifications and it can correct them...This works because the ModelState records
all user wrote. If for instance the user wrote 1o instead of 10 in a numeric filed it will see back its 1o so it can correct it.
Now if you deleted some rows....this mechanism will continue working but deleyed rows...are lost no way to recover them for the user....
So you have to recumpute the hidden fileds containing the indexes...since indexes changed....
That said I don't see any problem with this!! You can recompute them automatically, if you built them in the View...when you call again the same View...the indexes will be computed automatically again.
Thanks, what I am trying to do, build a model list dynamically use Jquery this works, it uses a nonsequencial list to allow deletion, and I use ajax to send that list to the controller, this works.
Then based on that list, I need to build a list of another model, attatch certain models from each list together, then send the list back to the view and sort out the form names, so when I received the first model everything is connected as one model, so
in my view I need to be able to line up the indexs to the list which is still in the view.
Something like I have three models
model1 which has a list naviagtion property to model 2
model 2 has a navigation property to model1 and model 3
and model 3 has a navigation property to model 2
my crud view has model 1 as default, we dynamically build model 2, and then send model 2 to the controller with ajax, we build a list of model3's in the controller, add the correct model 2s, then send that to a partial view, in the partial view I display
all model 3s, but need to add the indexs to the model 2's which are still there, so when I submit the form I don't have 2 model 2 lists.
When ypu send model 2 edited by the user via ajax...indexes must be considered part of it. So when your ajax call return back results, and new html is created, some old Html must be deleted too. Among the old Html to be deleted there are also the indexes...They
must be substituted to conform to the new model 2. In fact now deleted rows ghas already been taken into account and the model has been compacted...so the olde indexes that are still referring to the rows that have been deleted must be substituted.
So you don't have to line up with previous indexes, but just to substituthe them when the call return. How? With exactly the same code you used to create them the first time!
That said, I personally don't like toomuch the technique of the indexes used to handle list deletions, because the mapping that they define is based on their position in the html tree, so one must pay a lot of attention in avoiding to change their order
when manipulating the Html. In fact exchanging the position of two indexes in the page would cause the associated rows to be exchanged when the page is posted. However, the only other option is writing a custom model binder in an not easy way.
Once you have solved your problem...start considering what to doin case of errors. Errors handling is quite difficult when dynamic structures are involved if you need to process the whole user input into an unique transaction....You need to "rememer" the
whole set of changes done by the user while asking himto correct his errors, so once errors are corrected you can pass all changes together into an unique transaction.
When ypu send model 2 edited by the user via ajax...indexes must be considered part of it. So when your ajax call return back results, and new html is created, some old Html must be deleted too. Among the old Html to be deleted there are also the indexes...They
must be substituted to conform to the new model 2. In fact now deleted rows ghas already been taken into account and the model has been compacted...so the olde indexes that are still referring to the rows that have been deleted must be substituted.
So you don't have to line up with previous indexes, but just to substituthe them when the call return. How? With exactly the same code you used to create them the first time!
That said, I personally don't like toomuch the technique of the indexes used to handle list deletions, because the mapping that they define is based on their position in the html tree, so one must pay a lot of attention in avoiding to change their order
when manipulating the Html. In fact exchanging the position of two indexes in the page would cause the associated rows to be exchanged when the page is posted. However, the only other option is writing a custom model binder in an not easy way.
Once you have solved your problem...start considering what to doin case of errors. Errors handling is quite difficult when dynamic structures are involved if you need to process the whole user input into an unique transaction....You need to "rememer" the
whole set of changes done by the user while asking himto correct his errors, so once errors are corrected you can pass all changes together into an unique transaction.
I see what your saying, however at present I am not removing the first list which are being submitted with ajax, as they are build in a different area, and form part of the overall model, i was hoping just to link them up without a problem, and the only issue
is the indexing.
I could replace it, but I would need to rethink the sructure of my page, as its complicated enough at the moment, but will look into it.
If you don't like indexs in list definitions, how would you recommend I deal with them?
I have worked hard to make sure the ajax is strongly type and the unobrusive form validation works for the dynamic content, I will work some additional features in to refine it after, the thing I am having trouble with is ensureing that there are no database
errors making sure the records we are updating do belong to the model and haven't been altered manually.
Indexes have to be considered like private variables of your html representation of your list so you Must not rely on them when adding new data to your list. you should wtite js functions that do the job of adding new data to the ith row of your list. Thei
should work something like this: 1) do some jquery to locate the html node that contains the data of the ith row. 2) the root node of the ith row should contain the nane prefix common to all input fields of the ith row in some data-something htm5 attribute.
3) take the above prefix and substitute it in the newly received html to a placeholder youv created on the server side 4) add the new html connected to the ith row where appropriate this is the correct way....because on the server side you cannot know the
actual status of the indices to create adequate prefixes or adequate parallel indices.....not very easy. Placeholders are just string that cannot appear in your html...such as a guid followed by the index of the row....the right prefix will be substituted
on the client. A simpler alternative is the use of json communication...that would avoid the indices...but then you have to write custom code to handle server side errors returne to the lient together with the json. In my mvc controls toolkit I have grids...that
might help you and tools to handle smartly json communication.
Its a little over my head at the moment, if you had a link to a tutorial i would like to see a working example to get my head around it.
I understand that the indexs are an issue, however I have design my system so that the order isn't important, and the indexs don't get deleted so it should be stable, but I worry about every index which links models as it can change the code in the same
way you can change ids.
It also seems alot of work just to keep nested models inplace, I see alot of people just put the main model then all nested models they use ajax to load and update one at a time and I didn't like that.
EnenDaveyBoy
Participant
1465 Points
1147 Posts
Model Binding to non sequencial list issue
Apr 30, 2012 06:25 PM|LINK
Hi
I have a list which uses a non squencial list as in the link below:
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
However I need to send the list to the controller using ajax, then send the list of models back to to the view, and be able to set the same indexs, in my view, I have the list of models in my controller no problems, but how do I access and set the index in my view.
Any suggestions would be appriciated.
ignatandrei
All-Star
135210 Points
21690 Posts
Moderator
MVP
Re: Model Binding to non sequencial list issue
May 01, 2012 04:24 AM|LINK
if you want ajax, see what the normal post is sending and send the same.
francesco ab...
All-Star
20912 Points
3279 Posts
Re: Model Binding to non sequencial list issue
May 01, 2012 10:50 AM|LINK
Not well understood what you want to do. Namely....
If there were no errors you simply do ModelState.Clear() and repeate the same computations you have done ro render the original page. That is you recompute your indexes as you have done the first time.
Now the problem arise if there were errors.....Normally if you just pass back the model as you received in the view...WITHOUT PERFORMING ModelState.Clear() User will see just its modifications and it can correct them...This works because the ModelState records all user wrote. If for instance the user wrote 1o instead of 10 in a numeric filed it will see back its 1o so it can correct it.
Now if you deleted some rows....this mechanism will continue working but deleyed rows...are lost no way to recover them for the user....
So you have to recumpute the hidden fileds containing the indexes...since indexes changed....
That said I don't see any problem with this!! You can recompute them automatically, if you built them in the View...when you call again the same View...the indexes will be computed automatically again.
Mvc Controls Toolkit | Data Moving Plug-in Videos
EnenDaveyBoy
Participant
1465 Points
1147 Posts
Re: Model Binding to non sequencial list issue
May 01, 2012 10:56 PM|LINK
Hi
Thanks, what I am trying to do, build a model list dynamically use Jquery this works, it uses a nonsequencial list to allow deletion, and I use ajax to send that list to the controller, this works.
Then based on that list, I need to build a list of another model, attatch certain models from each list together, then send the list back to the view and sort out the form names, so when I received the first model everything is connected as one model, so in my view I need to be able to line up the indexs to the list which is still in the view.
Something like I have three models
model1 which has a list naviagtion property to model 2
model 2 has a navigation property to model1 and model 3
and model 3 has a navigation property to model 2
my crud view has model 1 as default, we dynamically build model 2, and then send model 2 to the controller with ajax, we build a list of model3's in the controller, add the correct model 2s, then send that to a partial view, in the partial view I display all model 3s, but need to add the indexs to the model 2's which are still there, so when I submit the form I don't have 2 model 2 lists.
I hope that sort of explains it
EnenDaveyBoy
Participant
1465 Points
1147 Posts
Re: Model Binding to non sequencial list issue
May 01, 2012 10:57 PM|LINK
francesco ab...
All-Star
20912 Points
3279 Posts
Re: Model Binding to non sequencial list issue
May 02, 2012 08:39 AM|LINK
Not understood exactly...but it is not important.
When ypu send model 2 edited by the user via ajax...indexes must be considered part of it. So when your ajax call return back results, and new html is created, some old Html must be deleted too. Among the old Html to be deleted there are also the indexes...They must be substituted to conform to the new model 2. In fact now deleted rows ghas already been taken into account and the model has been compacted...so the olde indexes that are still referring to the rows that have been deleted must be substituted.
So you don't have to line up with previous indexes, but just to substituthe them when the call return. How? With exactly the same code you used to create them the first time!
That said, I personally don't like toomuch the technique of the indexes used to handle list deletions, because the mapping that they define is based on their position in the html tree, so one must pay a lot of attention in avoiding to change their order when manipulating the Html. In fact exchanging the position of two indexes in the page would cause the associated rows to be exchanged when the page is posted. However, the only other option is writing a custom model binder in an not easy way.
Once you have solved your problem...start considering what to doin case of errors. Errors handling is quite difficult when dynamic structures are involved if you need to process the whole user input into an unique transaction....You need to "rememer" the whole set of changes done by the user while asking himto correct his errors, so once errors are corrected you can pass all changes together into an unique transaction.
Mvc Controls Toolkit | Data Moving Plug-in Videos
asteranup
All-Star
30184 Points
4906 Posts
Re: Model Binding to non sequencial list issue
May 02, 2012 08:58 AM|LINK
Hi,
Have a look at the post-
http://growingtech.blogspot.in/2012/03/binding-posting-to-dictionary-in-mvc-3.html
Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
EnenDaveyBoy
Participant
1465 Points
1147 Posts
Re: Model Binding to non sequencial list issue
May 02, 2012 05:14 PM|LINK
I could replace it, but I would need to rethink the sructure of my page, as its complicated enough at the moment, but will look into it.
If you don't like indexs in list definitions, how would you recommend I deal with them?
I have worked hard to make sure the ajax is strongly type and the unobrusive form validation works for the dynamic content, I will work some additional features in to refine it after, the thing I am having trouble with is ensureing that there are no database errors making sure the records we are updating do belong to the model and haven't been altered manually.
francesco ab...
All-Star
20912 Points
3279 Posts
Re: Model Binding to non sequencial list issue
May 02, 2012 07:29 PM|LINK
Mvc Controls Toolkit | Data Moving Plug-in Videos
EnenDaveyBoy
Participant
1465 Points
1147 Posts
Re: Model Binding to non sequencial list issue
May 02, 2012 10:46 PM|LINK
Its a little over my head at the moment, if you had a link to a tutorial i would like to see a working example to get my head around it.
I understand that the indexs are an issue, however I have design my system so that the order isn't important, and the indexs don't get deleted so it should be stable, but I worry about every index which links models as it can change the code in the same way you can change ids.
It also seems alot of work just to keep nested models inplace, I see alot of people just put the main model then all nested models they use ajax to load and update one at a time and I didn't like that.