[英]Pass ID from view of type IEnumerable using HTML Actionlink in Razor
我有两个模型“计算机”和“计算机详细信息”。 用户可以从“计算机的索引”页面中单击给定记录的“查看详细信息”,该记录将重定向到“计算机详细信息的索引”,并使用查询字符串获取与给定的“计算机ID”相关联的所有记录-正常工作
我的问题是我想要一个“创建”链接,该链接带有此“计算机ID”(在视图中显示)并填充“创建”表单上的“计算机ID”字段。
我使用了Model.First()。ComputerID来运行带有一些测试记录的代码,但是如果ComputerID的记录为null,则它当然不起作用。
视图
@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; }
}
这里发生了两件事:
首先,如果您的视图需要获取ComputerID,则可能需要在ViewModel中反映此字段,而不是从列表的第一项中获取该字段。
public class ComputerDetailsViewModel
{
public int ComputerID {get; set;} // populate in the action directly
public IEnumerable<MyApp.Models.ComputerDetail> Items {get; set;}
}
您确实想走这条路,应该使用nullable运算符进行更好的错误处理,以弄清ComputerID为null时会发生什么。
@Html.ActionLink("Create New", "Create", "ComputerDetail", new {id = Model.FirstOrDefault()?.ComputerID ?? 0 /*Null case*/}, new { @class = "btn btn-default"})
希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.