所以,这让我的第一个演示MVC Project感到困扰.在我的主视图MyAction.cshtml中,当您单击“学生用户名”超链接时,包含学生详细信息的局部视图会在StudentDetails< div>中呈现.
这是我的主要观点:
@model IEnumerable<MvcApplication4.ViewModels.StudentViewModel>
@{
ViewBag.Title = "MyAction";
Layout = "~/Views/Shared/_myTemplateLayoutPage.cshtml";
}
<script>
function RegisterClickHanders() {
var url = '@Url.Action("DisplayClickedView","Private")';
$('.editDetails').click(function () {
var btnvalue = $('.editDetails').attr("value");
var srchvalue = $(this).closest("tr").children().first().text();
$('#Add_Update_Details').load(url, { searchText: btnvalue, searchValue: srchvalue });
});
}
</script>
<div id="content">
<div id="mainpage">
<h2>Registration Details</h2>
<ul>
@foreach(var item in Model)
{
<li>
@Ajax.ActionLink(item.UserName,
"GetUserDetails",
new { id = item.Student.StudentId },
new AjaxOptions
{
UpdateTargetId = "StudentDetails",
InsertionMode = InsertionMode.Replace,
HttpMethod = "GET",
OnComplete="RegisterClickHanders"
}
)
</li>
}
</ul>
<div id ="StudentDetails"></div>
<div id ="Add_Update_Details"></div>
</div>
<div id="sidebarbottom"></div>
</div>
这是呈现的局部视图:
@model MvcApplication4.ViewModels.StudentViewModel
<h2>Student Details</h2>
<table border="1">
<tr>
<th>
Name
</th>
<th>
User Name
</th>
<th>
Department
</th>
<th colspan="2">
Actions
</th>
</tr>
<tr>
<td>
@Html.DisplayFor(x => x.StudentFullName)
</td>
<td>
@Html.DisplayFor(x => x.UserName)
</td>
<td>
@Html.DisplayFor( x => x.DepartmentName)
</td>
<td>
<input type="submit" class="editDetails" value="Edit Details" name="Command" />
</td>
<td>
<input type="submit" class="addDetails" value="Add Details" name="Command" />
</td>
</tr>
</table>
现在,我从StudentViewModel显示的详细信息也包含StudentId.这是StudentViewModel类:
//View Model
public class StudentViewModel
{
public Student Student { get; set; }
public int StudentId { get; set; }
[Display(Name = "StudentName")]
[Required(ErrorMessage = "Student Name cannot be left blank")]
public String StudentFullName
{
get
{
return String.Format("{0} {1}", Student.FirstName, Student.LastName);
}
}
public String UserName { get; set; }
[DataType(DataType.Password)]
public String Password { get; set; }
[DataType(DataType.Password)]
[Compare("Password",ErrorMessage="Passwords do not match")]
[Display(Name="Confirm Password")]
public String C_Password { get; set; }
[Display(Name = "Department Name")]
public String DepartmentName { get; set; }
}
但是我只在视图中显示StudentFullName,UserName和DepartmentName.
[HttpGet]
public PartialViewResult GetUserDetails(int? id)
{
StudentContext context = new StudentContext();
var result = context.Students.Where(s => s.StudentId == id)
.Include(s => s.Department)
.Select(s => new StudentViewModel
{
Student = s,
UserName = s.UserName,
DepartmentName = s.Department.DepartmentName
}).First();
return PartialView("_StudentDetails",result);
}
但是,当单击“编辑详细信息”按钮时,我希望为该记录呈现一个视图.我想做的是:
var url = '@Url.Action("DisplayClickedView","Private")';
$('.editDetails').click(function () {
var btnvalue = $('.editDetails').attr("value");
var srchvalue = $(this).closest("tr").children().first().text();
$('#Add_Update_Details').load(url, { searchText: btnvalue, searchValue: srchvalue });
});
但这将在srchvalue中获得StudentFullName,并基于此将呈现部分视图.但是我希望它基于视图中不存在的StudentId本身来呈现.
如何将StudentId传递给动作DisplayClickedView,以便根据ID本身进行更新或编辑?
解决方法:
当我想在html属性中具有一些值并在javascript中访问它时,我喜欢使用data-*
属性.
因此,您可以执行以下操作:
<输入type =“ submit” class =“ editDetails” value =“编辑详细信息” name =“命令” data-student-id =“ @ Model.StudentId” />
并在您的JavaScript中获取它:
var studentId = $('.editDetails').data("student-id");
这样,您不必在表内添加新的“隐藏”列,也无需更改按钮的默认属性.