I'm following off the same problem as posted here. However i'm getting the partial view results in the view except that on the click of the button the partial view results shows and goes away instantly.
Model Class
public class Role
{
public string Employee_ID { get; set; }
public string CPR { get; set; }
public string Dept_Name { get; set; }
public string Employee_Name { get; set; }
//User Role
public string UserRoleName { get; set; }
}
Main View Class
This is the main view class that accepts Employee_ID as an input and returns 3 model class as output i.e. CPR, Dept_Name and Employee_Name
public ActionResult CPanelDeleteRole()
{
return View();
}
[HttpPost]
public ActionResult CPanelDeleteRole(Role rol)
{
GetUserInfo(rol);
return View(rol);
}
public void GetUserInfo(Role rol)
{
connection();
SqlCommand com = new SqlCommand("GetUserRoleInfo", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@EmployeeID", rol.Employee_ID);
com.Parameters.Add("@EmployeeName", SqlDbType.NVarChar, 50);
com.Parameters.Add("@CPR", SqlDbType.Int, 12);
com.Parameters.Add("@DeptName", SqlDbType.NVarChar, 50);
com.Parameters["@EmployeeName"].Direction = ParameterDirection.Output;
com.Parameters["@CPR"].Direction = ParameterDirection.Output;
com.Parameters["@DeptName"].Direction = ParameterDirection.Output;
con.Open();
com.ExecuteNonQuery();
con.Close();
rol.Employee_Name = com.Parameters["@EmployeeName"].Value.ToString();
rol.CPR = com.Parameters["@CPR"].Value.ToString();
rol.Dept_Name = com.Parameters["@DeptName"].Value.ToString();
}
Partial View Class
This is the partial view class that accepts the Employee_ID input value and returns as string parameter for querying a list
public ActionResult TableData(string searchText)
{
connection();
var userrole = new List<Role>();
string query = "SELECT USER_ROLES.Role_ID, ROLES_LOOKUP.Role_Type FROM HelpDesk.dbo.ROLES_LOOKUP, HelpDesk.dbo.USER_ROLES WHERE USER_ROLES.Username = @EmployeeID AND USER_ROLES.Role_ID = ROLES_LOOKUP.Role_ID";
string constr = ConfigurationManager.ConnectionStrings["HelpDeskDBContext"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@EmployeeID", searchText);
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
userrole.Add(new Role
{
UserRoleName = sdr["Role_Type"].ToString(),
UserRoleID = Convert.ToInt32(sdr["Role_ID"].ToString())
});
}
}
con.Close();
}
}
return PartialView("_TablePartial", userrole);
}
View
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="header">
<h4 class="title">Delete Role</h4>
</div>
<div class="content">
<div class="row">
<div class="col-md-3">
<div class="form-group">
@Html.TextBoxFor(m => m.Employee_ID, new { @class = "form-control" })
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label></label>
<button id="GetRoleTable" class="form-control btn btn-danger btn-fill pull-left">submit</button>
<div class="clearfix"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label>الاسم</label>
<input type="text" class="form-control" placeholder="یوزرنیم" value="@Html.DisplayFor(m => m.Employee_Name)">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>الرقم الشخصي</label>
<input type="text" class="form-control" placeholder="یوزرنیم" value="@Html.DisplayFor(m => m.CPR)">
</div>
</div>
</div>
<div class="row">
<div class="col-md-5">
<div class="form-group">
<label>الشعبة</label>
<input type="text" class="form-control" placeholder="یوزرنیم" value="@Html.DisplayFor(m => m.Dept_Name)">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div id="AddRoleTable"></div>
</div>
</div>
}
<script type="text/javascript">
var url = '@Url.Action("TableData", "Admin")';
$('#GetRoleTable').click(function() {
var empID = $('#Employee_ID').val();
$('#AddRoleTable').load(url, { searchText: empID });
})
</script>
Partial View
@model IEnumerable<ITHelpDesk.Models.Role>
<table>
<tr>
<th>Role</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.UserRoleName)</td>
</tr>
}
</table>