如何在ASP .Net MVC中从Razor视图传递id到控制器的动作方法?

4

所以,这是我在我的第一个演示MVC项目中遇到的问题。在我的主视图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; }

}

但是我在视图中只显示学生全名、用户名和部门名称。
     [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,并基于此呈现部分视图。但我希望它基于学生ID本身进行呈现,而在视图中并不存在该ID。

enter image description here

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

请问您能展示一下吗?我不明白如果表格中没有学生ID,我该如何获取学生ID本身。 - StrugglingCoder
3个回答

3
当我想要在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");

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


1
StudentID添加到您的视图模型中。然后,您可以通过Razor在按钮上添加一个包含学生ID的href
@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>
            <a class="btn btn-default btn-sm" href="@Url.Content(~/Controller/Action")/@(x => x.StudentID)" type="submit">Edit</a>
        </td>
        <td>
            <a class="btn btn-default btn-sm" href="@Url.Content(~/Controller/Action")/@(x => x.StudentID)" type="submit">Add Details</a>
        </td>
    </tr>
</table>

在这里,控制器和操作将被替换为您的控制器和操作。


0
<input type="hidden" name="id" value="@Model.Id" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接