Sal*_*eer 1 c# asp.net asp.net-mvc asp.net-mvc-4
我现在在学习MVC 4方面遇到了一些麻烦,我希望有人可以提供帮助.据我所知,搜索中出现的问题没有重复,但如果有,请指导我.
我的问题是:在我在index.aspx页面上显示的学校列表中,是否有可能让用户点击"详细信息"链接,以便它返回并显示该特定学校的详细信息?我已经在下面的代码中包含了所有相关信息,但请原谅我是否太多(我不确定究竟需要什么).
如您所见,ActionResult Index()会返回我数据库中所有学校的列表.我想要做的是在他们点击"详细信息"时回发,以便视图返回有关该学校的特定信息.这可能吗?我认为有可能只传递被点击的学校的Id,但是当我回到DAC层时,它接受Id(int)作为参数,当我希望它返回一个"学校"类型的对象.这有意义吗?
对于它的价值,我知道一点Javascript,但没有jQuery,JSON等(还).
所以我有5层:
我的观点对象:
SchoolVO.cs:
public int Id { get; set; }
public string Name { get; set; }
public string Slogan { get; set; }
Run Code Online (Sandbox Code Playgroud)
我的数据库访问类:
SchoolDAC.cs
public static School GetSchool(School school)
{
try
{
using (SqlConnection conn = ConnectionHelper.GetConnection("SchoolDB"))
{
SqlCommand cmd = new SqlCommand("Schools.GetSchool", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@SchoolId", school.Id);
using (SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.Read())
{
school = readRecord(dr);
// readRecord() is just another method that fills the 3 properties of the school object with data from the database.
}
}
return school;
}
}
catch (Exception ex)
{
throw new Exception("Failed to get school", ex);
}
}
Run Code Online (Sandbox Code Playgroud)
我的"业务"层:
SchoolBus.cs:
public static School GetSchool(School school)
{
school = SchoolDAC.GetSchool(school);
return school;
}
Run Code Online (Sandbox Code Playgroud)
我的控制器:
SchoolController.cs:
public ActionResult Index()
{
List<School> model = SchoolBus.GetAllSchools();
return View(model);
}
[HttpPost]
public ActionResult Index(School school)
{
School model = SchoolBus.GetSchool(school);
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
我的看法:
index.aspx:
<table>
<tr>
<th>
<%: Html.DisplayNameFor(model => model.Id) %>
</th>
<th>
<%: Html.DisplayNameFor(model => model.Name) %>
</th>
<th>
<%: Html.DisplayNameFor(model => model.Slogan) %>
</th>
<th></th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%: Html.DisplayFor(modelItem => item.Id) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Name) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Slogan) %>
</td>
<td> <!--- possible to pass an object through here? --->
<%: Html.ActionLink("Sign Up", "Index", new { id=item.Id }) %> |
<%: Html.ActionLink("Details", "Details", new { id=item.Id }) %> |
<%: Html.ActionLink("Delete", "Delete", new { id=item.Id }) %>
</td>
</tr>
<% } %>
</table>
Run Code Online (Sandbox Code Playgroud)
你不应该将整个学校的对象传递给控制器.我将向您展示我在项目中所做的类似方法:
[HttpGet]
public ActionResult Edit(int id)
{
DbUser editedDbUser = context.Users.Single(x => x.UserId == id);
User editeduser = Mapper.Map<DbUser, User>(editedDbUser);
return View("AccountInfo", editeduser);
}
Run Code Online (Sandbox Code Playgroud)
在我的观点中,我知道这是剃须刀,但想法是一样的:
<tr>
<td>@(i + 1)</td>
<td>@usersModel[i].LastName</td>
<td>@usersModel[i].FirstName</td>
<td>@usersModel[i].Email</td>
<td>@Html.ActionLink("Edit", "Edit", new { id = @usersModel[i].UserId })</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
要回答你的问题,这是我的模型 - 它只是我在传递给视图之前填充的List.
public class Users
{
public List<User> UsersBag { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
如果您的模型中没有Id,则必须具有另一个属性,通过该属性可以区分列表中的对象,但我建议将其保留为某种ID.
归档时间: |
|
查看次数: |
16924 次 |
最近记录: |