将参数传递给Html.ActionLink
本文关键字:ActionLink Html 参数传递 | 更新日期: 2023-09-27 17:58:11
我正试图将一个参数传递给Index视图中的Create@Html.ActionLink,但遇到了一些困难。
我有一个地址控制器,用户在访问"索引"视图时可以在其中看到特定人员的地址。我想把这个PersonID传递给Create视图,这样他们在创建新地址时就不必选择或输入这个人。我的操作链接看起来像这样-
@Html.ActionLink("Create New", "Create", new { id = Model.[PersonID is not a choice]})
我的问题是,在模型之后,PersonID不是一个选项。我不知道如何在AddressController中为Create函数获取PersonID。
当模型是IEnumerable<T>-他们有同样的问题。第一个答案似乎是一个可能的解决方案,但我无法复制他们创建的模型,当我把这些片段放在地址模型中时,我也无法复制他们在控制器中执行的代码。还有其他解决方案吗?
我的地址模型-
public partial class Address
{
public int AddressID { get; set; }
public int PersonID { get; set; }
[Display(Name="Address")]
public string Address1 { get; set; }
[Display(Name = "Address 2")]
public string Address2 { get; set; }
public string City { get; set; }
[Display(Name="State")]
public string StateAbbr { get; set; }
[Display(Name = "Zip Code")]
public string Zip { get; set; }
public virtual Person Person { get; set; }
public List<Address> Address { get; set; }
}
我的AddressController索引
public ActionResult Index(int id)
{
var addresses = db.Addresses.Include(a => a.Person)
.Where(a => a.PersonID == id);
Person person = db.People.Find(id);
ViewBag.FullName = person.FirstName + " " + person.LastName;
person.PersonID = id;
return View(addresses.ToList());
}
地址索引视图-
@model IEnumerable<OpenBurn.Models.Address>
@{
ViewBag.Title = "Address";
}
<h2>Address</h2>
<p>
@Html.ActionLink("Create New", "Create", new { id = .PerosnID })
</p>
<h3 style="color: #008cba;"> @ViewBag.FullName </h3>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Address1)
</th>
<th>
@Html.DisplayNameFor(model => model.Address2)
</th>
<th>
@Html.DisplayNameFor(model => model.City)
</th>
<th>
@Html.DisplayNameFor(model => model.StateAbbr)
</th>
<th>
@Html.DisplayNameFor(model => model.Zip)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Address1)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address2)
</td>
<td>
@Html.DisplayFor(modelItem => item.City)
</td>
<td>
@Html.DisplayFor(modelItem => item.StateAbbr)
</td>
<td>
@Html.DisplayFor(modelItem => item.Zip)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.AddressID }) |
@Html.ActionLink("Details", "Details", new { id=item.AddressID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.AddressID })
</td>
</tr>
}
</table>
由于@Html.ActionLink
语法不在foreach
循环中,因此显然不能使用IEnumerable<OpenBurn.Models.Address>
作为模型。您需要一个新的模型类,该类包含一个保存这些Address记录的属性和一个保存PersonID
值的属性。您还应该使用相同的模型类来传递FullName
值,而不是使用ViewBag
。我建议使用以下型号的
public class AddressIndexModel
{
public AddressIndexModel()
{
this.Addresses = new List<Address>();
}
public int PersonID { get; set; }
public string FullName { get; set; }
public List<Address> Addresses { get; set; }
}
然后把你的控制器换成这个
public ActionResult Index(int id)
{
var addresses = db.Addresses.Include(a => a.Person)
.Where(a => a.PersonID == id);
Person person = db.People.Find(id);
AddressIndexModel model = new AddressIndexModel();
model.PersonID = id;
model.FullName = person.FirstName + " " + person.LastName;
model.Addresses = addresses.ToList();
return View(model);
}
并将您的视图更改为低于
@model AddressIndexModel
@{
ViewBag.Title = "Address";
}
<h2>Address</h2>
<p>
@Html.ActionLink("Create New", "Create", new { id = Model.PersonID })
</p>
<h3 style="color: #008cba;"> @Model.FullName </h3>
<table class="table">
<tr>
<th>
Address
</th>
<th>
Address 2
</th>
<th>
City
</th>
<th>
State
</th>
<th>
Zip Code
</th>
<th></th>
</tr>
@foreach (var item in Model.Addresses) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Address1)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address2)
</td>
<td>
@Html.DisplayFor(modelItem => item.City)
</td>
<td>
@Html.DisplayFor(modelItem => item.StateAbbr)
</td>
<td>
@Html.DisplayFor(modelItem => item.Zip)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.AddressID }) |
@Html.ActionLink("Details", "Details", new { id=item.AddressID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.AddressID })
</td>
</tr>
}
</table>