使用Razor中的HTML Actionlink从IEnumerable类型的视图传递ID

时间:2017-08-21 19:38:59

标签: c# asp.net-mvc-4 razor

我有两个型号"计算机"和"计算机详细信息。"在计算机的索引页面中,用户可以单击"查看详细信息"对于给定记录,该记录重定向到计算机详细信息的索引并获取与给定记录相关的所有记录"计算机ID"使用查询字符串 - 工作正常

我的问题是我希望有一个"创建"链接带有这个"计算机ID" (在视图中显示)并填充“创建”表单上的“计算机ID”字段。

我使用了 Model.First()。ComputerID ,它运行带有一些测试记录的代码,但当然,如果ComputerID的记录为空,它就不起作用。

查看

@modelIEnumerable<MyApp.Models.ComputerDetail>
@{
     ViewBag.Title = "Index";
 }

 <h2 class ="page-header">Computer Details</h2>

 <div class ="col-lg-12">
     <p>
           @Html.ActionLink("Create New", "Create", "ComputerDetail", new {id = Model.First().ComputerID}, new { @class = "btn btn-default"})
     </p>
<div class="panel panel-default">
    <div class ="panel-body">
        <table class ="table table-striped" id="dtaTable">
            <thead class ="dataTableHead">
                <tr>
                    <th>
                        @Html.DisplayNameFor(model => model.ComputerID)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.EmployeeID)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.StartDate)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.EndDate)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.Comments)
                    </th>
                    <th>Actions</th>
               </tr>
        </thead>

    @foreach (var item in Model) {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.ComputerID)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Employee.FullName)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.StartDate)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.EndDate)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Comments)
                    </td>
                    <td>
                        @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
                        @Html.ActionLink("Details", "Details", new { id=item.ID }) |
                        @Html.ActionLink("Delete", "Delete", new { id=item.ID })
                    </td>
                </tr>
}
            </table>
        </div>
    </div>
</div>

控制器

public ActionResult Index(int id)
    {
        var _FindComputerID = GetComputerDetails(id);
        return View(_FindComputerID);
    }

    private List<ComputerDetail>GetComputerDetails(int id)
    {
        var FindComputerID = db.ComputerDetails.Where(cd => cd.ComputerID == id).Include
                    (cd => cd.Employee).OrderByDescending(cd => cd.ID);

        return FindComputerID.ToList();
    }

    [HttpGet]
    public ActionResult Create(int id)
    {
        ComputerDetail computerdetail = new ComputerDetail ();
        computerdetail.ComputerID = id;

        return View(computerdetail);
    }

模型

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

    [Required(ErrorMessage = "Please enter service tag")]
    [Display(Name = "Service Tag")]
    public int ComputerID { get; set; }

    [Required(ErrorMessage = "Please select employee name")]
    [Display(Name = "Employee Name")]
    public int EmployeeID { get; set; }

    [Required(ErrorMessage = "Please enter date")]
    [Display(Name = "Date Bought")]
    [DataType(DataType.Date)]
    public DateTime StartDate { get; set; }

    [Display(Name = "Date Bought")]
    [DataType(DataType.Date)]
    public DateTime? EndDate { get; set; }

    public string Comments { get; set; }

    //references
    public virtual Assets.Computer Computer { get; set; }
    public virtual Employee Employee { get; set; }

}

1 个答案:

答案 0 :(得分:0)

这里发生了两件事:

首先,如果您的视图需要获取ComputerID,您可能希望在ViewModel中反映此字段,而不是神奇地从列表中的第一项获取它。

public class ComputerDetailsViewModel
{
    public int ComputerID {get; set;} // populate in the action directly
    public IEnumerable<MyApp.Models.ComputerDetail> Items {get; set;}
}

你真的想要走这条路,你应该使用可空运算符来更好地处理错误,弄清楚当ComputerID为空时会发生什么。

@Html.ActionLink("Create New", "Create", "ComputerDetail", new {id = Model.FirstOrDefault()?.ComputerID ?? 0 /*Null case*/}, new { @class = "btn btn-default"})

希望这有帮助!