繁体   English   中英

将参数传递给Html.ActionLink

[英]Passing a parameter to Html.ActionLink

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

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

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

我的问题是,在Model PersonID之后不是一个选择。 我不确定如何将PersonID转到AddressController中的Create函数。

我尝试了跟帖一起- 在模型为IEnumerable <T>时将参数传递给Html.ActionLink-在这里他们遇到相同的问题。 第一个答案似乎是一个可能的解决方案,但是我无法复制他们创建的模型,并且当我将片段放入地址模型中时,我无法复制他们在控制器中执行的代码。 还有其他解决方案吗?

我的地址模型-

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>作为模型。 您需要一个新的模型类,该类包含一个包含那些地址记录的属性和一个包含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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM