Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a model with property named List<place>
place has property like
placename,
placedescription

now I have two textboxes to enter placename and description, after that I'm clicking add button and those data inserted in a table tbody just below the Add button by jquery. So table row is increasing.

<table id="tblPlacelist">
<thead>
<tr>
<th>Place Name</th>
<th>Place Description</th>
</tr>
</thead>
<tbody></tbody>
</table>


Now the thing is, how can I pass the table row data as list?

I have taken a partial razor view and the list<place> declared above of the view.

What I have tried:

The same problem what described above.
Posted
Updated 24-Jan-17 18:01pm
Comments
Afzaal Ahmad Zeeshan 24-Jan-17 14:52pm    
Tables are generally used to render the results.

I can only suggest that you take the data and create a sample object from it. You can try to read the row and column values, and then have a simple JavaScript object. Then send it to the controller.
souvikcode 24-Jan-17 23:10pm    
I'm very new in MVC. I need table here because user can delete row,add row to table by javascript. I don't know any other good way to make a collection in front end. So if you can post any sample code,that will be better.

Create jquery function to pass the table data into contoleer



function SaveData() {

//Create array object to push the table data into it.
var selected = new Array();
//Push the table data into it.

$('#table_bodyID tr').each(function () {
selected.push({
JsonObject1: $tr.find('td:eq(0) input[type=hidden]').val(),
JsonObject2:$tr.find('td:eq(1) input[type=Text]').val()
});
});

$.ajax
({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "ContollerName/ActionMethodname/",
data: JSON.stringify({ ActionMethodParamterName: selected }),
dataType: "json",
success: function (data) {
alert('Success')
},
error: function (x, e) {
alert('Failed')
}
});

}

Note: Here JsonObject1, JsonObject2 are the properties of the model which you are passing the data from view to controller.
 
Share this answer
 
Comments
souvikcode 25-Jan-17 2:37am    
This is same as when we pass data from ajax to asp webmethod. Its fine. But I have to post more data to that controller with that table.
Say, hallName,Hall description is property of a class hallmaster. and table will go to property "hall_place_link",which is list. Now you can understand I hope. In this case I have to post the full model in ajax?
souvikcode 25-Jan-17 5:06am    
Its not working when I call this function on the button click which is posting the data to controller.
Ramesh Kumar Barik 25-Jan-17 5:09am    
What is the issue?

1. Controller method is not called?
2. Or any issue in ajax call.
souvikcode 25-Jan-17 5:15am    
see,I have created a javascript function like this

function saveData(model) {
alert('hi');
$("#tblPlaceList").find("tbody").find("tr").each(function () {
model.SelectHall.hall_Place_link.push({
hallPlaceId: $(this).find("td:eq(0)").html(),
placeName: $(this).find("td:eq(1)").html(),
placeDescription: $(this).find("td:eq(2)").html(),
avDrawing: "",
hallId: model.SelectHall.hallId
});
});

$.ajax
({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/Hall/Insert",
data: JSON.stringify({ obj: model }),
dataType: "json",
success: function (data) {
alert('Success')
},
error: function (x, e) {
alert('Failed')
}
});
}

first of all how can I pass the model parameter from view to that javascript function. And will this be done by the button which is doing postback or I have to prevent the postback.
Ramesh Kumar Barik 25-Jan-17 5:32am    
function saveData(varhallId) {
var hall_Place_link = new Array();
$("#tblPlaceList").find("tbody").find("tr").each(function () {
hall_Place_link.push({
hallPlaceId: $(this).find("td:eq(0)").html(),
placeName: $(this).find("td:eq(1)").html(),
placeDescription: $(this).find("td:eq(2)").html(),
avDrawing: "",
hallId: varhallId
});
});

$.ajax
({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/Hall/Insert",
data: JSON.stringify({ obj: hall_Place_link }),
dataType: "json",
success: function (data) {
alert('Success')
},
error: function (x, e) {
alert('Failed')
}
});
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900