0

I need help if it is possible. I have a list of classrooms, as it is in the picture below. I want to list all teachers in the "select option" and when the user selects the teacher he can click save and the classroom will be updated, as it is in the href. How can I take the value after the foreach loop?

  <select id="teachers">
    @foreach(var teacher in @Model.Teachers){
      <option value="@teacher.Id">@teacher.Id</option>
    }
    </select>
    
    <a asp-controller="Classroom" asp-action="Update" asp-route-teacherId=""></a>

enter image description here

Robert
  • 15
  • 6
  • Perhaps you may need to use different approach. Instead of using ``, use JavaScript to construct /fire the hyperlink. Reference [trigger event on selected option](https://stackoverflow.com/questions/26885738/trigger-event-on-selected-option), [Dynamically Create Link Javascript](https://stackoverflow.com/questions/9831074/dynamically-create-link-javascript), [How do I create a link using javascript?](https://stackoverflow.com/questions/4772774/how-do-i-create-a-link-using-javascript) – Thiam Hooi Jun 30 '21 at 00:23

1 Answers1

0
<a asp-controller="Classroom" asp-action="Update" asp-route-teacherId="XX">Save</a>

First, for the above a tag helper, the generated HTML like this:

<a href="/Classroom/Update?teacherId=XX">Save</a> 
or  
<a href="/Classroom/Update/XX">Save</a>

Besides, to update teachers in specific classrooms, you should also submit the classroom id to the Update action method, so, the generated URL should have multiple parameters(teacherid and classroomId), like this: <a href="/Classroom/Update?teacherId=XX&classroomId=XXX">Save</a>

More detail information, see Anchor Tag Helper.

I have a list of classrooms, as it is in the picture below. I want to list all teachers in the "select option" and when the user selects the teacher he can click save and the classroom will be updated, as it is in the href. How can I take the value after the foreach loop?

From your description, each classroom (row) should have a <select> element to choose the teacher and a "Save" button to update the current row update, right?

In this scenario, you could use the select element's change event to get the selected value, and then update the <a> tag href attribute.

You can refer the following sample:

Model:

public class ClassRoom
{
    public int ID { get; set; }
    public string Classroom { get; set; }
    public string SubJect { get; set; }
    public string Teacher { get; set; }
    public DateTime Date { get; set; }

}
public class Teacher
{
    public int Id { get; set; }
    public string Name { get; set; }
}
public class Subject
{
    public int Id { get; set; }
    public string SubjectName { get; set; }
}
public class ClassRoomViewModel
{
    public List<ClassRoom> ClassRooms { get; set; }
    public List<Teacher> Teachers { get; set; }
    public List<Subject> Subjects { get; set; }
}

Controller:

public class ClassRoomController : Controller
{
    public IActionResult Index()
    {
       // you could query the database to get the data. The following is the test data.
        var viewmodel = new ClassRoomViewModel();
        viewmodel.ClassRooms = new List<ClassRoom>()
        {
            new ClassRoom(){ ID=1, Classroom="EOIS1", Date=DateTime.Now },
            new ClassRoom(){ ID=2, Classroom="EOIS2", Date=DateTime.Now }
        };
        viewmodel.Teachers = new List<Teacher>()
        {
            new Teacher(){ Id=101, Name="Tom"},
            new Teacher(){ Id=102, Name="Jack"}
        };

        return View(viewmodel);
    }

    public IActionResult Update(int teacherId, int classroomid)
    {
        //update the classroom

        //redirect to the Index page and refresh the page.
        return RedirectToAction(nameof(Index));
    }
}

View Page:

@model MVCWebApplication.Models.ClassRoomViewModel 
<table class="table" id="customers" >
    <thead>
         ...
    </thead>
    <tbody>
@foreach (var item in Model.ClassRooms) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.ID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Classroom)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SubJect)
            </td> 
            <td>
                <select id="teachers" class="teachers">
                    <option value="0">select teacher</option>
                    @foreach (var teacher in @Model.Teachers)
                    {
                        <option value="@teacher.Id">@teacher.Name</option>
                    }
                </select>
            </td>
            <td>
                <a asp-controller="ClassRoom" asp-action="Update" asp-route-classroomid="@item.ID" asp-route-teacherId="">Save</a>
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Date)
            </td> 
        </tr>
}
    </tbody>
</table>

@section Scripts{ 
<script> 
    $(function () {
        $(".teachers").each(function (Index, item) {
            $(item).change(function () {
                var teacherid = $(this).val(); //get the selected value.
                var existinghref = $(item).parent().parent().find("a").attr("href");  //get the hyperlink href attribute.
                if (teacherid != 0) {
                    existinghref = existinghref + "&teacherId=" + teacherid; //add the parameter at the end of the request url.
                    $(item).parent().parent().find("a").attr("href", existinghref); //update the hyperlink href attribute.
                }
                else {
                    alert("Please select teacher"); //show prompt to let user select teacher.
                }
            });
        });
    });
</script> 
}

The result as below:

enter image description here

Zhi Lv
  • 18,845
  • 1
  • 19
  • 30