使用 Razor 中的 HTML 操作链接从 IEnumerable 类型的视图传递 ID



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

我的问题是我想要一个带有此"计算机 ID"(显示在视图中(的"创建"链接并填充"创建"表单上的"计算机 ID"字段。

我使用了Model.First((。用于运行带有一些测试记录的代码的计算机 ID,但如果 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;}
}

您确实想走这条路,您应该使用可为空的运算符进行更好的错误处理,弄清楚当 ComputerID 为空时会发生什么。

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

希望这有帮助!

最新更新