【发布时间】:2014-09-11 08:28:50
【问题描述】:
我想在同一页面内单击视图链接时在主视图内显示部分视图。请帮助我,我是MVC的新手。除了使用 Ajax.ActionLink() 之外,还有其他方法吗?
这是我的添加细节控制器。
public ActionResult DisplayDetails()
{
SqlConnection connect = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ToString());
connect.Open();
DataSet ds = ExecuteQuery("select EmployeeID, EmployeeName, EmailID from EmployeeDetails");
IList<AddDetails> lst = new List<AddDetails>();
AddDetails ad;
foreach (DataRow dr in ds.Tables[0].Rows)
{
ad = new AddDetails();
ad.EmployeeId = Convert.ToInt32(dr["EmployeeID"]);
ad.EmployeeName = dr["EmployeeName"].ToString();
ad.EmailId = dr["EmailID"].ToString();
lst.Add(ad);
}
connect.Close();
return PartialView("DisplayDetails", lst);
}
这是主视图 添加详细视图(主视图)。
@model mvcEmployeeTask.Models.AddDetails
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
@using (Html.BeginForm("AddDetail", "AddDetails", FormMethod.Post))
{
<table>
@* <tr>
<td>
@(Html.Label("Employee ID : "))
</td>
<td>
@Html.TextBoxFor(model => model.EmployeeId)
@Html.HiddenFor(model => model.EmployeeId)
</td>
</tr>*@
@Html.HiddenFor(model => model.EmployeeId)
<tr>
<td>
@(Html.Label("Employee Name : "))
</td>
<td>
@(Html.TextBoxFor(model => model.EmployeeName))
</td>
</tr>
<tr>
<td>
@(Html.Label("Email ID : "))
</td>
<td>
@(Html.TextBoxFor(model => model.EmailId))
</td>
</tr>
<tr>
<td>
<input type="submit" value="submit" name="Add" />
</td>
<td>
@* @Html.ActionLink("View", "DisplayDetails")*@
@Html.ActionLink("View", "DisplayDetails")
</td>
</tr>
</table>
@Html.Action("DisplayDetails", "AddDetails");
}
这里是局部视图(显示视图)。
@model IList<mvcEmployeeTask.Models.AddDetails>
@{
Layout = null;
}
@using (Html.BeginForm("DisplayDetails", "AddDetails", FormMethod.Get))
{
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>DisplayDetails</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
</head>
<body> <div class="table" align="center">
<table border="1" >
<tr>
<th>EmployeeID</th>
<th>EmployeeName</th>
<th>Email</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@item.EmployeeId
</td>
<td>
@item.EmployeeName
</td>
<td>
@item.EmailId
</td>
<td>
@Html.ActionLink("Edit", "EditDetails", new { id= item.EmployeeId })
</td>
<td>
@Html.ActionLink("Delete", "DeleteDetails", new { id= item.EmployeeId })
</td>
</tr>
}
</table>
</div>
</body>
</html>
}
请帮帮我!!
【问题讨论】:
-
首先将您的操作链接更正为@Html.ActionLink("DisplayDetails", "View") 并且为了在点击操作链接时加载部分内容,然后您可以使用 jquery(ajax 请求)
-
好的,有没有其他不用jQuery的方法?有一个类似的问题。网址如下:stackoverflow.com/questions/7295835/…
-
不使用 ajax 助手和 jquery.. 我认为不可能..
-
你能帮帮我吗?正如你提到的,我已经进行了更正。
-
您的主视图有一个比部分视图更简单的模型(这是主视图具有的单个项目的列表)。这似乎不正确(似乎倒退了)。你能澄清一下吗?
标签: asp.net-mvc asp.net-mvc-4 razor asp.net-mvc-partialview