gpt4 book ai didi

c# - 如何将 id 从 Razor View 传递到 ASP .Net MVC 中的 Controller 操作方法?

转载 作者:太空狗 更新时间:2023-10-30 01:16:17 25 4
gpt4 key购买 nike

所以,这就是我的第一个演示 MVC 项目困扰我的地方。在我的主视图 MyAction.cshtml 中,当您单击 Student UserName 超链接时,包含 Student Details 的部分 View 将呈现在 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>

这是渲染的部分 View :

@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; }

}

但我只在 View 中显示 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);

}

但是当单击“编辑详细信息”按钮时,我希望为该记录呈现一个 View 。我想做的是:

   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,并基于此呈现部分 View 。但我希望根据 View 中不存在的 StudentId 本身呈现它。

enter image description here

我如何将 StudentId 传递给操作 DisplayClickedView,以便根据 ID 本身进行更新或编辑?

最佳答案

当我想在 html 属性中有一些值并在 javascript 中访问它时,我喜欢使用 data-* 属性。

所以,你可以这样做:

<input type="submit" class="editDetails" value="Edit Details" name="Command" data-student-id="@Model.StudentId"/>

并在您的 javascript 中获取它:

var studentId = $('.editDetails').data("student-id");

这样您就不必在表格中添加新的“隐藏”列,也不必更改按钮的默认属性。

关于c# - 如何将 id 从 Razor View 传递到 ASP .Net MVC 中的 Controller 操作方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35728062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com