Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
have a page that searches employees by campus. The campuses are populated in a select using razor. I then get all the filtered employees but the page refuses to redraw the DOM with the current number of employees

I have an onchange() of the select it calls an ajax that will call the ActionResult of the page. This somehow works fine since it returns the correct data.

What I have tried:

A select populated using razor
HTML
<select placeholder="Select campus" id="selCampus" class="form-control" required>
                                <option value="">Select campus</option>

                                @foreach (var campus in Model.Campuses)
                                {

                                    <option value="@campus.Id">@campus.CampusName</option>

                                    }
                                </select>

Onchange for the select pointing to the action controller
JavaScript
$("#selCampus").change(function () {
                //$(".employee").remove();
                $.ajax({
                    type: 'POST',
                    url: '/admin/staff/',
                    dataType: 'html',
                    data: ({
                        campusId: $(this).val()
                    }),
                    success: function () {
                    }
                });
            });


ActionResult, this loads on pageload and onchange of the select
C#
public ActionResult Employees(long? campusId)
                {
                    var client = new RestClient(Request.Url.GetLeftPart(UriPartial.Authority).ToString());
                    var request = new RestRequest("api/employee/getClinicMembers/{campusId}", Method.POST);
                    request.AddParameter("campusId", campusId, ParameterType.QueryString);

                    var result = client.Execute<ClinicMembersOutput>(request);

                    ViewBag.title = "Home | Members";
                    return View(result.Data);
                }


Does not want to redraw this code with new data, Even tho Model.Employees has filtered data
HTML
@foreach (var member in Model.Employees)
                        {
                            <div class="col-lg-4 col-xlg-3 col-md-5 employee">
                                <div class="card">
                                    <div class="card-body">

                                    </div>
                                </div>


                            </div>

                        }
Posted
Updated 22-Apr-19 13:04pm

success: function () {
                    }


Your code IS empty. You're making an AJAX call. That means, your Razor doesn't run again, you need to write code to populate your values, in javascript.
 
Share this answer
 
Your foreach is "empty"; there's only "markup"; nothing showing from "var member" in Model.EMployees.

ScottGu's Blog - ASP.NET MVC 3: Razor’s @: and <text> syntax[^]
 
Share this answer
 
Comments
Anele Ngqandu 22-Apr-19 15:56pm    
Hi Gerry, no it's not empty, the filtering works but somehow the redrawing of the div is not happening.
[no name] 22-Apr-19 23:54pm    
Your "Div" is "empty" ... it is "blank" ... it has no "presence" ... it is not visible to the human eye ... content is nada ... nada is nada ... nada

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