I have a loop that creates a button for each entry. However, I need to set a dynamic id for the button in order to use it and know the selected entry upon button click.
My controller retrieves a list of entries from the database and using "ViewBag.List", I added the buttons. The list derrived from the database includes ID, Title and Description.
This is the view/ Index.cshtml
@model Exams.Models.subjects
@{
ViewBag.Title = "Subjects"
}
<h2> Current Subjects </h2>
<form method="post" action="@Url.Action("Submit")" >
<table>
@foreach(var subject in ViewBag.List){
<tr>
<td>
@subject.Title </br>
@subject.Desc
</td>
<td>
<button name="Submit"> Submit </button>
<td>
<tr>
}
</table>
Upon clicking the button, the next view must include "You just registered " + SubjectTitle
How should the subject Title be included? Knowing that simple ViewBag saves the SubjectTitle by reference
You can set dynamic id to button in this way
Use a View Model to persist your data. View models are great. Here are a few reasons why.
Use whatever controller code you're using to fill
ViewBag.List
to fillSubjects
instead and return the view with theSubjectsViewModel
, something like:Your new view:
From the HTML I changed, I will assume you're not massively used to Razor. You can read up on Razor here.
You can then do this with your controller:
This tells the controller to pass the value of the button you named as 'submit'. If you wanted to rename it, just change the name of the input and the parameter in the
POST
controller action.3 Things,
1)You need to attach click event to button.
2) use,
$(this).parent().prev()
in clicked handler to target previous td element.3) get the td html, split on occurrence of br and get the first element for getting title only.
Working Demo