MVC Razor View中的Ajax ActionLink

时间:2016-02-27 22:25:13

标签: jquery ajax asp.net-mvc asp.net-mvc-4 razor

在我的演示MVC应用程序中,我有一个学生和一个部门模型类。

public class Student
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int StudentId { get; set; }
    public String LastName { get; set; }
    public String FirstName { get; set; }
    public String UserName { get; set; }
    public String Password { get; set; }

    [ForeignKey("Department")]
    public int DepartmentID { get; set; }

    //Department Navigational Property
    public Department Department { get; set; }
}

 public class Department
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int DepartmentID { get; set; }

    [Required(ErrorMessage="Department Name cannot be empty")]
    public String DepartmentName { get; set; }

    [Required(ErrorMessage = "Department Code cannot be empty")]
    public String DepartmentCode { get; set; }

    public virtual ICollection<Student> Students { get; set; }
}

StudentViewModel看起来像:

    public class StudentViewModel
{
    public Student Student { 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; }

}

StudentEFInitializer类中的初始值设定项数据为:

    protected override void Seed(StudentContext context)
    {
        var departments = new List<Department>
        {
            new Department {DepartmentName="CSE",DepartmentCode="CS101"},
            new Department {DepartmentName="ECE",DepartmentCode="EC197"},
            new Department {DepartmentName="MECH",DepartmentCode="MC202"}
        };

        departments.ForEach(d => context.Departments.Add(d));
        context.SaveChanges();

        var students = new List<Student> {
            new Student {FirstName = "Zack",LastName="Dyas", UserName ="zack_dyas",DepartmentID=1,Password ="zack123"},
            new Student {FirstName = "Abraham",LastName="D'Pedro",UserName ="ab_dpedro",DepartmentID=2,Password ="ab123"}
        };

        students.ForEach(s => context.Students.Add(s));
        context.SaveChanges();

    }
}

现在在我的操作视图MyAction.cshtml中,首先使用ajax.actionlink向他们展示所有注册的用户名。

@model IEnumerable<MvcApplication4.ViewModels.StudentViewModel>  
@{
    ViewBag.Title = "MyAction";
    Layout = "~/Views/Shared/_myTemplateLayoutPage.cshtml";
}


<div id="content">
    <div id="mainpage">

    <h2>Registration Details</h2>
        <ul>
        @foreach(var item in Model) 
        {
            <li>
                @Ajax.ActionLink(item.UserName, @*Text to be displayed *@
                    "GetUserDetails", @*Action Method Name*@
                    new AjaxOptions
                    {
                       UpdateTargetId = "CustomerDetails", @*DOM element ID to be updated *@
                       InsertionMode = InsertionMode.Replace,@*Replace the content of DOM element *@
                       HttpMethod = "GET" @*HTTP method *@
                    }
                )
            </li>
        }
        </ul>
        <div id ="CustomerDetails">
            <h2>Student Details</h2>
            <table>
                <tr>
                    <th>Student Name</th>
                    <th>User Name</th>
                    <th>Department</th>
                </tr>
                <tr>

                </tr>
            </table>
        </div>
    </div>
    <div id="sidebar">
    </div>
    <div id="sidebarcontents">
        <ul id="menu">
            <li>
                <h2>Our Services</h2>
            </li>
            <li>
                <ul>
                    <li><a href="#">Link a   Website</a></li>
                    <li><a href="#">Website Link B</a></li>

                    <li><a href="#">Website Link C</a></li>
                    <li><a href="#">Small Businesses Website</a></li>
                    <li><a href="#">Flyer Website</a></li>
                </ul>
            </li>
        </ul>
    </div>

    <div id="sidebarbottom"></div>
</div>

问题是,当我点击用户名的超链接时,假设“zack_dyas”

enter image description here

我不知道如何从数据库中获取ID(即StudentID = 1),并在ID为“CustomerDetails”的div中显示与该注册学生相关的所有相关详细信息。

我无法弄清楚代码。我是MVC的新手。

0 个答案:

没有答案