Click here to Skip to main content
15,068,929 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello Everyone,
I have Created a partial view and getting Employee data with Employees Image on load.
I have a dropdownlist of state, So I want data should be display based on selected state.
I need it in urgent, Please help me

Thanks in Advance

What I have tried:

It's Partial View
@model OnlineApp.Models.DBEntities.EmployeeDetail

<div class="row" id="@Model.EmpId" style="margin-bottom:25px; border:outset; background-color:antiquewhite">
    <div @*class="image"*@ style="margin-bottom:20px">
        <div class="col-md-6">
            <img class="img-responsive" src="~/Images/@Model.ImagePath" height="450" width="600" />
        </div>
        <div class="col-md-6">
            <h3 style="color:navy">@Model.EmpName</h3>
            <h5>(@Model.Department)</h5>
            <h4>Contact No : @Model.Mobile1</h4>
            <div style="align-items:flex-start">
                <h5>State : @Model.State</h5>
                <h5>City : @Model.City</h5>
                <h5>Address : @Model.Address1</h5>
            </div>
            <button type="button" id="@Model.EmpId" onclick="EditEmp(@Model.EmpId)" style="margin-bottom:5px" class="btn btn-primary">Edit Employee</button>
        </div>
    </div>
</div>


Index View
@model IEnumerable<OnlineApp.Models.DBEntities.EmployeeDetail>
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
    @foreach (var item in Model)
    {
        @Html.Partial("_Employee", item);
    }


Ajax Call
$("#ddlState").change(function () {
        var id = $(this).val();
        if (id > 1) {
            getCity(sid = { "sid": id });
            cid = "0";
            sid = id.toString();
            dataObject = JSON.stringify({
                "sid": sid,
                "cid": cid,
            });
            GetEmp();
        }
    });

    function GetEmp() {
        $.ajax({
            data: dataObject,
            url: '/Employee/Index', type: "POST", dataType: "json",
            contentType: 'application/json',
            success: function (result) {
            }
        })
    }


This is Controller Method
public ActionResult Index(string sid, string cid)
       {
           var result = bandRepo.GetEmployees(sid, cid);
           return View(result.ToList());
       }
Posted
Updated 7-Feb-19 6:58am
Comments
j snooze 5-Feb-19 17:44pm
   
Are you getting a error or anything? I display a partial view myself. Something like this. Based on an activity dropdown change, I ask a question. The jquery basically builds the partial view and grabs the html, then injects it into my main form.

$(document).ready(function () {
$('#ddlActivityType').change(function () {
/* Get the selected value of dropdownlist */
var selectedID = $(this).val();
var questionUrl = $(this).data('qt') + '/';

/* Request Questions the partial view with .get request. */
$.get(questionUrl + selectedID, function (data) {
$('#questions').html(data);
$('#questions').fadeIn('fast');
});
});
Member 12611488 6-Feb-19 13:21pm
   
I have tryied same as you but not getting proper output, data is not refreshing, it showing the same data as loaded first time.
Also not getting any error.


<div id="PartialContainer">
    @foreach (var emp in Model)
    {
        @Html.Partial("_Employee", emp);
    }
</div>



function GetEmp() {
debugger;
$.ajax({
data: dataObject,
url: '/Employee/Index', type: "POST", dataType: "json",
contentType: 'application/json',
success: function (result) {
$("#PartialContainer").html(result);
}
})
}
F-ES Sitecore 7-Feb-19 7:35am
   
That code is putting the html into an element with the id of "PartialContainer" but you have no such element on your page. In the page that houses your partial view you'll need to put a div with that id around it;

<div id="PartialContainer">
@Html.Partial...
</div>

Also if you get 500 errors that is your controller action failing which is a different issue. You'll need to do some debugging to find out what the error is.
Member 12611488 8-Feb-19 0:06am
   
This is working for me but the problem is I'm not getting Image, If Image will display It will be fine for me.

AJAX Call
function GetEmp() {
        $.ajax({
            data: dataObject,
            url: '/Employees/GetEmployees', type: "POST", dataType: "json",
            contentType: 'application/json',
            success: function (result) {
                var divison;
                for (var i in result) {
                    divison = "<div class='row' style='margin-bottom:25px; border:outset; background-color:antiquewhite'><div class='image' style='margin-bottom:20px'><div class='col-md-6'> <img class='img-responsive' src='~/Images/" + result[i].ImagePath + "' height='450' width='600' /></div><div class='col-md-6'><h3 style='color:navy'>" + result[i].EmpName + "</h3><h4>Contact No : " + result[i].Mobile1 + "</h4><div style='align-items:flex-start'><h5>State : " + result[i].State + "</h5><h5>City : " + result[i].City + "</h5><h5>Address : " + result[i].Address1 + "</h5></div><button type='button' id=" + result[i].EmpId + " onclick='EditDetail(" + result[i].EmpId + ")' class='btn btn-primary'>Edit Detail</button></div></div></div>"
                    $('#PartialContainer').append(divison);
                }   
            }
        })
    }


Action Method
[HttpPost]
public ActionResult GetEmployees(string sid, string cid)
{
    var result = empRepo.GetEmployees(sid, cid);
    return Json(result, JsonRequestBehavior.AllowGet);
}


Any Help will be appreciated. Thanks
Member 12611488 6-Feb-19 23:22pm
   
Some times I'm getting Error "Internal Server Error 500"

1 solution

Actually that Element I have but it's missing while posting

<div id="PartialContainer">
@foreach (var item in Model)
{
    @Html.Partial("_Employee", item);
}
</div>


Ajax Call
function GetEmp() {
$.ajax({
data: dataObject,
url: '/Employees/GetEmployees', type: "POST", dataType: "json",
contentType: 'application/json',
success: function (result) {
$("PartialContainer").html(result);
})
}


Action Method
[HttpPost]
public ActionResult GetEmployees(string sid, string cid)
{
    var result = empRepo.GetEmployees(sid, cid);
    return Json(result, JsonRequestBehavior.AllowGet);
}
   
v2

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