将参数传递给Html.ActionLink

时间:2016-07-22 15:54:09

标签: c# asp.net-mvc

我正在尝试将参数传递给Index视图中的Create @ Html.ActionLink,但是我遇到了一些困难。

我有一个地址控制器,用户在访问索引视图时可以看到特定人员的地址。我想将此PersonID传递给Create视图,以便他们在创建新地址时不必选择或输入该人员。我的actionlink看起来像这样 -

@Html.ActionLink("Create New", "Create", new { id = Model.[PersonID is not a choice]})

我的问题是在模型PersonID不是一个选项之后。我不知道如何将PersonID添加到AddressController中的Create函数。

我尝试跟随帖子Passing a parameter to Html.ActionLink when the model is 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; }
}

我的地址控制器索引

 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>

1 个答案:

答案 0 :(得分:1)

由于@Html.ActionLink语法不在foreach循环中,因此您显然无法使用IEnumerable<OpenBurn.Models.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>