Spring mvc form submit not binding/updating the children objects [of list type] properly - spring

I have a data model that is something like this:
I have one parent entity A and it has one children of list type B. In order to display the data on JSP page, i am passing the object of entity A as model attribute..
I am able to display all the properties of entity A by binding the spring PATH attribute of the field. I have one section in JSP in which I am displaying the table of property (of type entity B) as it contains the multiple elements in the list of type B by iterating the elements using jstl for loop. Till this point everything is working as expected.
We have one functionality on JSP to add the new rows in the table of property type B. I have written one JS method to add the new row in table and also increment the index of the spring path attribute by 1. i.e. if the existing row path points to propertyB[0].name, then the newly added row's path will point to propertyB[1].name.
When I add a new row in the table and also update the existing row in the table. Upon form submission, I am getting the updated value of existing row in the controller but I don't get the newly added row as new element in controller.
What I need to do to get the newly added element in the object.

get the id from A.
get the data from the newly added B
send to controller by ajax
in the controller you can update database;
return an object in json in ajax;
judge the update is right or wrong;
if right use jquery to show the newly B ,if error,alert some something

Related

How to use same model with less fields for another view in mvc5

My Question is not belonging to how to use two models on same razor view ! basically I have a user table in which i have fields like(userid,name,email,password,gender,country,department,IsActive) and my form is working fine i am able to insert update and delete i have not use EF , what i did i create the table in sql server and create the model in my model folder , in my view i have put the required field validator for all these columns and they are mandatory to input while inserting or updating.
Now I want to have another view with another controller where i do not want to show all 8 fields instead want to show just these four columns( username,email,gender,IsActive)
when i am using the same model for the other controller and view then it loads the record correctly on index view ,but when i update the required it fires validation error as all my fields are mark as required so it ask to input the rest of four fields value as well.
I have tried to remove these un-necessary fields from model in controller code before saving using Bind([Exclude]"""") but it did not work.
I have tried modelstate.remove("") this approach works fine for all fields but my password field is still throwing validation error . someone says you need to use viewmodel and in viewmodel you have to put both of your model like the full model and small model, I want to ask how my small model would be mapped to my user table (as table["tableName"] this cannot be applied to two models and pointing to same table without primary foriegn key relation .
Share example please i am confused
modelState.Remove("Password")
This remove all model values which are un-necessary but not removing the password field which gives error while updating the
You have required fields missing data or some fields are not null when being saved. What you are trying to do is completely ok. You are basically using a Virtual Model. This is what I do. In the "get", you fix your model and send it to the view. You work with it and then when you submit, you receive the model in the "post", but it is missing several REQUIRED fields. So what you need to do is, retrieve the same record from the database, update the 3 or 4 fields from the view model and then save that model that you retrieved with the new data. This is one way. (VM = view model)
1. send VM to view
2. add data in the view
3. submit
4. receive VM in POST Controller
5. get same record from DB
6. update the particular fields using the VM data
7. save the updated record to database. This is the record you retrieved in 5.
Another way is to have all the missing fields in the model but not showing them in the view. However you need to mention them in the view, otherwise they will not post. so you need to use
#Html.Hidefor( x=> x.NameOfField)
This will ensure that the field is sent back for posting. Any field with a [Required]... is required. You cannot miss it!
How will your small model be mapped to the Database. It will not. You will transfer the data from the small model to the model that is mapped in the database... with ALL the fields. Either you get the missing values from the database or from the view by using Hidefor.
example
dbmodel.Field1 = vm.field1;
dbmodel.Field2 = vm.field2;
dbmodel.Field3 = vm.field3;
I hope this helps

creating dynamic records in single form using JS and store the info in a List or set object using spring mvc

I need to create form fields like name , age and city inside a form and this should happen every time i click add record. i dont want multiple forms all dynamic group should be created within a single form. on submitting the data should be taken by controller using modelattribute, and store in arraylist or any ...help out to find it. i have used following links and almost got it but unfortunately got deleted.
https://viralpatel.net/blogs/spring-mvc-multi-row-submit-java-list/
http://www.nobukimi.com/?p=237
answer
https://netjs.blogspot.com/2018/09/spring-mvc-binding-list-of-objects-example.html

KendoUI grid create : how to deal when server response only the id of the new created item

I want to use the KendoUI grid widget with a rest API (manage by dreamfactory); when i create a new item with the grid the server response contains only the new id and NOT the fully new item (aka all the fields) as I read in this forum http://www.telerik.com/forums/request-for-support-on-editable-grid#2098471.
"When you add a new item in the Grid its ID should be generated on the server and the newly inserted item (as an array) back to the client. This way the DataSource can update its internal data and the Grid widget will update the column for this field. In case the server does not return the result, the inserted item will be treated as a new one every time you sync the dataSource. The same applies for destroy and update functionality."
My question is how to deal with if we cannot modify the contains of the server response.
here is the response i get from the server : {"record":[{"id":26}]}
Any idea?
According to the DreamFactory documentation, only the id of the created record is returned by default. The docs also mention that you may select the returned fields. Using fields=* will return all created fields. You should be sure to use the record wrapper to force the return of an array rather than a single, unwrapped id field.

Table with Data from Two POJO Bean Data Controls

JDev 11.1.1.5.0
Use case:
Display a table where the first row will contain values from one POJO Bean data control and the second row will contain values from another.
I have managed to display the first row from one bean data control, now I want to show the second row with the other.
Hope the use case is clear.
Have a pojo that returns a collection that combines the data from the two pojos into a single record/object.
Then create a table where you put two fields in each column and show the details of the record that way.

problem with 'datatable' jquery plugin and two table (ajax related)

i have two tabs that their content loads by ajax. both have a table in their content. i want to apply 'datatable' jquery plugin to both table. tables have the same id because they are create by a function.but their rows are different.
datatable plugin is applied to first tab table well but on the second one give this error:
"DataTables warning (table id = 'dttable'): Cannot reinitialise DataTable.
To retrieve the DataTables object for this table, please pass either no arguments to the dataTable() function, or set bRetrieve to true. Alternatively, to destory the old table and create a new one, set bDestroy to true (note that a lot of changes to the configuration can be made through the API which is usually much faster)."
i use "bDestroy":true in datatable plugin define.but in this way the plugin doesn't show in second table.
would you help me?
Your problem is that both tables have the same ID, which is invalid HTML. When you try to initialize the second Databable, your selector only finds the first table and tries to initialize Datatables on the first table again, which results in the error that you are getting.
You need to change your function to create each table with a unique ID and initialize each table by its respective ID.
Why not set the Datatables by a className rather than ID then it can apply to both of them?
When retrieving the data you can use something like $('.dataTableStyle').eq(1) to get information from the relevant one.
I'm using mvc3 and my problem was with initializing a dataTable in a view, then rendering a partial view with another dataTable. The issue was not in the id's of the 2 tables, but in the way the partial views get rendered in the framework. In my case, I had to move the script, or reference to the script, into the view that hosts the partial view. I had an issue similar to this using the Google Maps api.
try this code
$(document).ready(function() {
oTable = $('#DataTables_Table_0').dataTable({ //DataTables_Table_0 <-------table id
iVote: -1, //your field name
"bRetrieve":true
});
oTable.fnSort( [ [1,'desc'] ] );
});
use this in function event when you would change your table data
$('#tbl_resultates').dataTable().fnDestroy();
and add
"bRetrieve": true,
in
$('#tbl_resultates').dataTable({

Resources