Hi. I have a page with all the records being listed. I have the records which can be uniquely identified because I have used @Html.HiddenFor for every record.
The 'id' for each row is unique: 1,2,3...
Now, if I want to take data to server side from Bootstrap modal popup then how can I use jQuery to get hold of a particular record that has unique ids and use $.ajax()?
I suggest you could use Partial View to get more data from database to view.
Then you could use model popup to show the Partial View when user click the details button.
$(function () {
$(".anchorDetail").click(function () {
var $buttonClicked = $(this);
var id = $buttonClicked.attr('data-id');
$.ajax({
type: "GET",
url: '/Authors/myModal',//add a Partial View in controller
contentType: "application/json; charset=utf-8",
data: { "Id": id },
datatype: "json",
success: function (data) {
$('#myModalContent').html(data);
$('#myModal').modal('show');
},
error: function () {
alert("Dynamic content load failed.");
}
});
});
});
If you have any questions,feel free to let me know and post more code so that I can reproduce your issue.
Best Regards.
Yuki Tao
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
What purpose does 'HiddenFor' serve in the context of performing CRUD?
It creates a hidden input on the form for the field (from your model) that you pass it.
It is useful for fields in your Model/ViewModel that you need to persist on the page and have passed back when another call is made but shouldn't be seen by the user.
As my opinion,It is an invisible container for storing data.
Use it is often related to your needs.
Best Regards.
Yuki Tao
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
I created the working code for your scenario where the data is taken from
View to Controller using jQuery AJAX method. Next, in the controller the
relevant record is fetched (from a repository which can be a database), then this record's data is returned to the View in
JSON format.
In my case it is Student.cs class which is added to the
Models folder.
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public int Standard { get; set; }
public string City { get; set; }
}
Creating a Controller
I created a controller called StudentController.cs inside the
Controllers folder of my project.
public class StudentController : Controller
{
// GET: Student
public ActionResult Index()
{
return View();
}
[HttpPost]
public JsonResult GetStudentData(int id)
{
List<Student> studentList = CreateStudents();
Student student = studentList.Where(a => a.Id == id).FirstOrDefault();
return Json(student);
}
public List<Student> CreateStudents()
{
List<Student> studentList = new List<Student>()
{
new Student(){ Id=1, Name="Ram", Age=11, Standard=9, City="New York" },
new Student(){ Id=2, Name="Jack", Age=8, Standard=5, City="New Jersey" },
new Student(){ Id=3, Name="Alex", Age=7, Standard=4, City="Boston" },
new Student(){ Id=4, Name="Pamela", Age=12, Standard=10, City="Albany" }
};
return studentList;
}
}
I will be calling this GetStudentData() action method from the
.ajax() jQuery method on the View. It will get the
student's id in it's parameter, then it searches the id in the student's repository. Finally it will return the corresponding student's record in
JSON format to the View.
You can see that inside the .ajax() method the Controller's Action called GetStudentData() is invoked. I am passing the data-id value of the
clicked anchor tag with my AJAX call like this - data: '{"id":"' + $(this).attr("data-id") + '"}'
Once the AJAX response is got the the student's information is shown inside the
bootstrap model.
Helping you always. Don't forget to click "Mark as Answer" on the post that helped you.
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
Member
4 Points
17 Posts
Bootstrap modal popup, HiddenFieldFor, MVC, EF
Mar 26, 2019 11:44 AM|uid282803|LINK
Hi. I have a page with all the records being listed. I have the records which can be uniquely identified because I have used @Html.HiddenFor for every record.
The 'id' for each row is unique: 1,2,3...
Now, if I want to take data to server side from Bootstrap modal popup then how can I use jQuery to get hold of a particular record that has unique ids and use $.ajax()?
Contributor
3710 Points
1431 Posts
Re: Bootstrap modal popup, HiddenFieldFor, MVC, EF
Mar 27, 2019 06:33 AM|Yuki Tao|LINK
Hi Wonderous,
According to requirement,I think this demo meet your need:
https://forums.asp.net/post/6209612.aspx
I suggest you could use Partial View to get more data from database to view.
Then you could use model popup to show the Partial View when user click the details button.
$(function () { $(".anchorDetail").click(function () { var $buttonClicked = $(this); var id = $buttonClicked.attr('data-id'); $.ajax({ type: "GET", url: '/Authors/myModal',//add a Partial View in controller contentType: "application/json; charset=utf-8", data: { "Id": id }, datatype: "json", success: function (data) { $('#myModalContent').html(data); $('#myModal').modal('show'); }, error: function () { alert("Dynamic content load failed."); } }); }); });
If you have any questions,feel free to let me know and post more code so that I can reproduce your issue.
Best Regards.
Yuki Tao
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
4 Points
17 Posts
Re: Bootstrap modal popup, HiddenFieldFor, MVC, EF
Mar 27, 2019 08:30 AM|uid282803|LINK
Hi. In the following statement:
I want to know if 'data-id' has already been there since start of asp.net.
Was there any other alternative that gives me 'id' on clicking button present in the row of a record?
What purpose does 'HiddenFor' serve in the context of performing CRUD?
Thanks!
Contributor
3710 Points
1431 Posts
Re: Bootstrap modal popup, HiddenFieldFor, MVC, EF
Mar 28, 2019 02:51 AM|Yuki Tao|LINK
Hi Wonderous,
No,This is just a custom attribute.You don't need to have to follow it completely.
Of course.
For example:
You could use onclick() event to pass id.
It creates a hidden input on the form for the field (from your model) that you pass it.
It is useful for fields in your Model/ViewModel that you need to persist on the page and have passed back when another call is made but shouldn't be seen by the user.
As my opinion,It is an invisible container for storing data.
Use it is often related to your needs.
Best Regards.
Yuki Tao
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Participant
1253 Points
943 Posts
Re: Bootstrap modal popup, HiddenFieldFor, MVC, EF
Apr 01, 2019 10:16 AM|yogyogi|LINK
Hello Friend,
I created the working code for your scenario where the data is taken from View to Controller using jQuery AJAX method. Next, in the controller the relevant record is fetched (from a repository which can be a database), then this record's data is returned to the View in JSON format.
In the View I am showing the data inside a Bootstrap Model.
This is how the code will work:
Creating a Repository
In my case it is Student.cs class which is added to the Models folder.
Creating a Controller
I created a controller called StudentController.cs inside the Controllers folder of my project.
public class StudentController : Controller { // GET: Student public ActionResult Index() { return View(); } [HttpPost] public JsonResult GetStudentData(int id) { List<Student> studentList = CreateStudents(); Student student = studentList.Where(a => a.Id == id).FirstOrDefault(); return Json(student); } public List<Student> CreateStudents() { List<Student> studentList = new List<Student>() { new Student(){ Id=1, Name="Ram", Age=11, Standard=9, City="New York" }, new Student(){ Id=2, Name="Jack", Age=8, Standard=5, City="New Jersey" }, new Student(){ Id=3, Name="Alex", Age=7, Standard=4, City="Boston" }, new Student(){ Id=4, Name="Pamela", Age=12, Standard=10, City="Albany" } }; return studentList; } }
I will be calling this GetStudentData() action method from the .ajax() jQuery method on the View. It will get the student's id in it's parameter, then it searches the id in the student's repository. Finally it will return the corresponding student's record in JSON format to the View.
Creating a View
Now add the View with the following code:
You can see that inside the .ajax() method the Controller's Action called GetStudentData() is invoked. I am passing the data-id value of the clicked anchor tag with my AJAX call like this - data: '{"id":"' + $(this).attr("data-id") + '"}'
Once the AJAX response is got the the student's information is shown inside the bootstrap model.
Rest of the code is easy to understand.
Reference - jQuery AJAX Complete Guide for Beginners and Experts – 7 Examples with Codes
Hope it helps you.
Regards...
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠