我正在解决与发布here相同的问题。但是我在视图中获得局部视图结果,只是在单击按钮时,局部视图结果显示并立即消失。
模型类
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; }
}
主视图类
这是主视图类,它接受Employee_ID作为输入并返回3个模型类作为输出,即CPR,Dept_Name和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();
}
部分视图类
这是部分视图类,它接受Employee_ID输入值并返回查询列表的字符串参数
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);
}
查看
@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>
部分视图
@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>