i have this List<ViewModel> through which i am populating a table so this table has many rows.
what i want to do is to add a Button in every row and when that button is clicked i need to pass data from that row to my controller using ajax
i can't seem to select values of single row and pass them to the controller using ajax this is what i am using
<script>
$('#captcha').on('click', function (e) {
var customer = {};
customer.U_ID = $("#some_property").val();
customer.P_ID = $("#some_property2").val();
// customer.P_ID = row.find("TD").eq(1).html();
//Send the JSON array to Controller using AJAX.
$.ajax({
type: "POST",
url: "/Admin/AjaxDeleteMethod",
data: JSON.stringify(customer),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
alert(r + " record(s) inserted.");
}
});
});
</script>
only the first row button takes me to the controller and i only get the first row values.
i need some way to send values of the rows of which button is clicked
namespace MvcBasic.Controllers
{
public class ItemViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public int Quantity { get; set; }
}
public class AjaxController : Controller
{
List<ItemViewModel> items;
public AjaxController()
{
items = new List<ItemViewModel>()
{
new ItemViewModel()
{
Id = 1,
Name = "Hello",
Quantity = 2
},
new ItemViewModel()
{
Id = 2,
Name = "World",
Quantity = 10
},
new ItemViewModel()
{
Id = 3,
Name = "Foo",
Quantity = 33
}
};
}
// GET: Ajax
[HttpGet]
public ActionResult Index()
{
return View(items);
}
[HttpPost]
public ActionResult Index(ItemViewModel Item)
{
return Json(Item);
}
}
}
var row = $(this).closest('tr');
var data = {
Id: $(row).find('td input[name*="Id"]').val(),
Name: $(row).find('td input[name*="Name"]').val(),
Quantity: $(row).find('td input[name*="Quantity"]').val()
};
thanks but this didn't work for me it only gets the result from the first row if i click the button on its end but for second row the click does nothing
i need to do two things first if button is clicked on the end of the row then pas this data via ajax to controller and do something and then there's a form submit too for sending remaining rows to the controller.
what i really am trying to do is to delete a record if clicked on and then submit the remaining rows
EDIT:-
your solution also works 100% fine appparently there's some issue with using ID instead of class because i was using ID value using # tag on click
it was working only for the firs row as soon as i changed it to class it started working for every row click.
but there's some weird issue...packagestatus value coming in ajaxdeletemethod is true while it should be false...can't figure out why its showing true instead of false
public class TestViewModel
{
public UserTestModel user{ get; set; }
public PackageTestModel package { get; set; }
public UserPackageTestModel userPackage { get; set; }
public TransactionTestModel transaction { get; set; }
}
public class CustomerViewModel
{
public int U_ID { get; set; }
public int P_ID { get; set; }
public int ID { get; set; }
public int PID { get; set; }
public string Name { get; set; }
public string Amount { get; set; }
public bool PackageStatus { get; set; }
public string ScreenShotPath { get; set; }
public int TransactionID { get; set; }
}
public class UserTestModel
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
}
public class PackageTestModel
{
[Key]
public int PID { get; set; }
public string PName { get; set; }
}
public class UserPackageTestModel
{
[Key]
public int U_ID { get; set; }
public int P_ID { get; set; }
public bool PackageStatus { get; set; }
}
public class TransactionTestModel
{
[Key]
public int TransactionID { get; set; }
public string ScreenShotPath { get; set; }
public string Amount { get; set; }
}
Controller
public class UserTestController : Controller
{
public static List<TestViewModel> testlist = new List<TestViewModel> {
new TestViewModel {
user= new UserTestModel { ID = 1, Name = "Name1" },
package= new PackageTestModel { PID = 1, PName = "PName1" },
userPackage= new UserPackageTestModel { P_ID =1, U_ID = 1, PackageStatus = true },
transaction= new TransactionTestModel { TransactionID = 1, Amount = "Amount1", ScreenShotPath = "~/Files/1.png" }
},
new TestViewModel {
user= new UserTestModel { ID = 2, Name = "Name2" },
package= new PackageTestModel { PID = 1, PName = "PName2" },
userPackage= new UserPackageTestModel { P_ID =2, U_ID = 2, PackageStatus = false },
transaction= new TransactionTestModel { TransactionID = 2, Amount = "Amount2", ScreenShotPath = "~/Files/1.png" }
},
new TestViewModel {
user= new UserTestModel { ID = 3, Name = "Name3" },
package= new PackageTestModel { PID = 1, PName = "PName3" },
userPackage= new UserPackageTestModel { P_ID =3, U_ID = 3, PackageStatus = true },
transaction= new TransactionTestModel { TransactionID = 3, Amount = "Amount3", ScreenShotPath = "~/Files/1.png" }
}
};
public ActionResult Index()
{
return View(testlist);
}
[HttpPost]
public ActionResult AjaxDeleteMethod(CustomerViewModel customer)
{
//do something to delete
var testViewModel = new TestViewModel();
testlist.ForEach(t => { if (t.user.ID == customer.ID) { testViewModel = t; } });
testlist.Remove(testViewModel);
return Json(customer,JsonRequestBehavior.AllowGet);
}
[HttpPost]
public ActionResult ActivatePackages(List<TestViewModel> test)
{
//do something
return RedirectToAction("Index");
}
}
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
thanks for so much effort but this works only for the first row..nothing happens if i click on anyother row delete button
EDIT:-
click is working now i think issue was that i was using button i changed it to input type=button and changed id=captcha to class=captcha
but now issue is that in controller when i have a simple model then it gets value fine for it(for eg if i use UserPackage model then it gets value fine)...but when i try to pass view model it show null for user,packages,transaction,userpackages
EDIT-2:-
your code works 100% fine i created a new model to get ll values from ajax to AjaxDeleteMethod but is there any way i can pass PendingPaymentApprovals model same one which i populating the table rows in the view?
public class PendingPaymentApprovalsModel
{
public User user { get; set; }
public UserPackage userPackage { get; set; }
public Transaction transaction { get; set; }
public Package package { get; set; }
}
and then access in AjaxDeleteMethod with transaction.transactionID,UserPackages.U_ID etc?
also package status is not showing correct value it shows true when it should be false and this remains the same whether checkbox value is checked or not but i think thats because its getting value from the model which is getting value from database and not
from the currently selected checkbox value
hen access in AjaxDeleteMethod with transaction.transactionID,UserPackages.U_ID etc?
Because you are using ajax to send an object called consumer,
not the PendingPaymentApprovalsModel. Therefore, UserPackages.U_ID cannot be used to get the value like this.
Learner94
checked or not but i think thats because its getting value from the model
According to what you mean here, the checkbox cannot be modified. You can modify the code like this.
@Html.CheckBoxFor(modelitem => Model[i].userPackage.PackageStatus, new { @disabled = "disabled" })
Best Regards,
YihuiSun
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
5 Points
58 Posts
how to send selected row data from view to controller using ajax
Sep 10, 2020 08:54 PM|Learner94|LINK
i have this List<ViewModel> through which i am populating a table so this table has many rows.
what i want to do is to add a Button in every row and when that button is clicked i need to pass data from that row to my controller using ajax
i can't seem to select values of single row and pass them to the controller using ajax this is what i am using
only the first row button takes me to the controller and i only get the first row values.
i need some way to send values of the rows of which button is clicked
All-Star
53121 Points
23675 Posts
Re: how to send selected row data from view to controller using ajax
Sep 10, 2020 11:14 PM|mgebhard|LINK
Below is an example. Unfortunately, you did not share the HTML which is the most important part...
Member
5 Points
58 Posts
Re: how to send selected row data from view to controller using ajax
Sep 11, 2020 01:27 AM|Learner94|LINK
thanks but this didn't work for me it only gets the result from the first row if i click the button on its end but for second row the click does nothing
this is the html
i need to do two things first if button is clicked on the end of the row then pas this data via ajax to controller and do something and then there's a form submit too for sending remaining rows to the controller.
what i really am trying to do is to delete a record if clicked on and then submit the remaining rows
EDIT:-
your solution also works 100% fine appparently there's some issue with using ID instead of class because i was using ID value using # tag on click
it was working only for the firs row as soon as i changed it to class it started working for every row click.
but there's some weird issue...packagestatus value coming in ajaxdeletemethod is true while it should be false...can't figure out why its showing true instead of false
Contributor
2770 Points
793 Posts
Re: how to send selected row data from view to controller using ajax
Sep 11, 2020 06:14 AM|YihuiSun|LINK
Hi Learner94,
According to your needs, I modified your code, you can refer to it.
More details, you could refer to below code:
Model
public class TestViewModel { public UserTestModel user{ get; set; } public PackageTestModel package { get; set; } public UserPackageTestModel userPackage { get; set; } public TransactionTestModel transaction { get; set; } } public class CustomerViewModel { public int U_ID { get; set; } public int P_ID { get; set; } public int ID { get; set; } public int PID { get; set; } public string Name { get; set; } public string Amount { get; set; } public bool PackageStatus { get; set; } public string ScreenShotPath { get; set; } public int TransactionID { get; set; } } public class UserTestModel { [Key] public int ID { get; set; } public string Name { get; set; } } public class PackageTestModel { [Key] public int PID { get; set; } public string PName { get; set; } } public class UserPackageTestModel { [Key] public int U_ID { get; set; } public int P_ID { get; set; } public bool PackageStatus { get; set; } } public class TransactionTestModel { [Key] public int TransactionID { get; set; } public string ScreenShotPath { get; set; } public string Amount { get; set; } }
Controller
View
Here is the result.
Best Regards,
YihuiSun
Member
5 Points
58 Posts
Re: how to send selected row data from view to controller using ajax
Sep 11, 2020 06:29 AM|Learner94|LINK
thanks for so much effort but this works only for the first row..nothing happens if i click on anyother row delete button
EDIT:-
click is working now i think issue was that i was using button i changed it to input type=button and changed id=captcha to class=captcha
but now issue is that in controller when i have a simple model then it gets value fine for it(for eg if i use UserPackage model then it gets value fine)...but when i try to pass view model it show null for user,packages,transaction,userpackages
EDIT-2:-
your code works 100% fine i created a new model to get ll values from ajax to AjaxDeleteMethod but is there any way i can pass PendingPaymentApprovals model same one which i populating the table rows in the view?
and then access in AjaxDeleteMethod with transaction.transactionID,UserPackages.U_ID etc?
also package status is not showing correct value it shows true when it should be false and this remains the same whether checkbox value is checked or not but i think thats because its getting value from the model which is getting value from database and not from the currently selected checkbox value
Contributor
2770 Points
793 Posts
Re: how to send selected row data from view to controller using ajax
Sep 15, 2020 08:27 AM|YihuiSun|LINK
Hi Learner94,
@Html.CheckBoxFor(modelitem => Model[i].userPackage.PackageStatus, new { @disabled = "disabled" })
Best Regards,
YihuiSun