2016-03-01 52 views
3

所以,这里有什么困扰我与我的第一个演示MVC项目。在我的主视图MyAction.cshtml中,单击Student UserName超链接时,包含Student Details的部分视图将在StudentDetails <div>中呈现。如何在ASP .Net MVC中将剃须刀视图中的ID传递给控制器​​的操作方法?

这里是我的主视图:

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

    } 

但是,当点击Edit Details按钮时,我想为该记录呈现一个视图。我所试图做的是:

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

但是,这会得到StudentFullName在srchvalue并基于该局部视图将呈现。但是我希望它能够基于StudentId本身呈现,而这在视图中并不存在。

enter image description here

我怎样才能通过StudentId到行动DisplayClickedView,以便更新或编辑偏偏根据id本身?

+0

你能证明吗?我无法理解,如果不在表格中,我将如何获得StudentId本身。 – StrugglingCoder

回答

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

这样你就不必添加新的 “隐藏”表格中的列既不会更改按钮的默认属性。

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

StudentID添加到您的视图模型中。然后你就可以通过剃刀其中包括学生证添加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> 

如果控制器和动作与您的控制器和动作所取代。

相关问题