Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery MVC Razor视图中的Ajax ActionLink_Jquery_Ajax_Asp.net Mvc_Asp.net Mvc 4_Razor - Fatal编程技术网

Jquery MVC Razor视图中的Ajax ActionLink

Jquery MVC Razor视图中的Ajax ActionLink,jquery,ajax,asp.net-mvc,asp.net-mvc-4,razor,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; } publi

在我的演示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; }
}

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

    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();

    }
}
受保护的重写无效种子(StudentContext上下文)
{
var部门=新列表
{
新部门{DepartmentName=“CSE”,DepartmentCode=“CS101”},
新部门{DepartmentName=“ECE”,DepartmentCode=“EC197”},
新部门{DepartmentName=“MECH”,DepartmentCode=“MC202”}
};
departments.ForEach(d=>context.departments.Add(d));
SaveChanges();
var students=新列表{
新学生{FirstName=“Zack”,LastName=“Dyas”,UserName=“Zack\u Dyas”,DepartmentID=1,Password=“zack123”},
新学生{FirstName=“Abraham”,LastName=“D'Pedro”,UserName=“ab_dpedro”,DepartmentID=2,Password=“ab123”}
};
ForEach(s=>context.students.Add);
SaveChanges();
}
}
现在,在我的action视图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>
@model IEnumerable
@{
ViewBag.Title=“MyAction”;
Layout=“~/Views/Shared/\u myTemplateLayoutPage.cshtml”;
}
注册详情
    @foreach(模型中的var项目) {
  • @ActionLink(item.UserName,@*要显示的文本*@ “GetUserDetails”,@*操作方法名称*@ 新选择 { UpdateTargetId=“CustomerDetails”,@*要更新的DOM元素ID*@ InsertionMode=InsertionMode.Replace,@*替换DOM元素的内容*@ HttpMethod=“GET”@*HTTP方法*@ } )
  • }
学生资料 学名 用户名 部门
  • 我们的服务
问题是当我点击用户名的超链接时,假设“zack_dyas”

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


我搞不懂密码。我是MVC新手。

视图中的模型是
StudentViewModel
,但您尚未显示它。它是否包含StudentID的属性?什么是
GetUserDetails()
方法(它需要一个ID参数,而您的链接需要将该值传递给该方法),我无法确定向该方法发送ID的内容和方式。基本上代码本身对我来说并不清楚。但问题中包含了视图模型定义。不确定从哪里开始(您对视图模型是什么以及如何使用它有一些误解)。您的方法需要是
GetUserDetails(int-id)
,并将学生的id传递给链接中的方法-例如
@Ajax.ActionLink(item.UserName,“GetUserDetails”,new{id=item.Student.StudentID,…
但是您视图中的模型应该是
@model IEnumerable
而不是
StudentViewModel
视图中的模型是
StudentViewModel
,但是您的视图中没有显示它。它是否包含StudentID的属性?以及
GetUserDetails()
方法是什么(它需要ID的一个参数,您链接需要将该值传递给该方法)我无法确定向该方法发送ID的内容和方式。基本上,我不清楚代码本身。但问题中包含视图模型定义。不确定从何处开始(您对什么是视图模型以及如何使用它有一些误解)。您的方法需要是
GetUserDetails(int-id)
,并且您将学生的id传递给链接中的方法-例如
@Ajax.ActionLink(item.UserName,“GetUserDetails”,new{id=item.Student.StudentID,…
但是视图中的模型应该是
@model IEnumerable
而不是
StudentViewModel
    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();

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