Ethel Patrick
Ethel Patrick

Reputation: 975

Passing a parameter to Html.ActionLink

I am trying to pass a parameter to the Create @Html.ActionLink in the Index view however I am having some difficulties.

I have an Address controller in which the user sees the addresses of a specific person when they access the Index view. I would like to pass this PersonID to the Create view so that they do not have to select or enter the person when they create a new address. My actionlink looks like this -

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

My problem is that after Model PersonID is not an option. I am not sure how to get PersonID to the Create function in the AddressController.

I tried following along with the post - Passing a parameter to Html.ActionLink when the model is IEnumerable<T> - where they are having the same issue. The first answers seems like a likely solution however I could not duplicate the model they created and when I put the pieces in my Address model I could not duplicate the code they had performed in the controller. Is there another solution?

My Address Model -

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; }
}

My AddressController Index

 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());
    }

Address Index view -

@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>

Upvotes: 0

Views: 5397

Answers (1)

ekad
ekad

Reputation: 14614

Since the @Html.ActionLink syntax isn't inside a foreach loop, you obviously can't use IEnumerable<OpenBurn.Models.Address> as the model. You need a new model class that contains a property that holds those Address records and a property that holds the PersonID value. You should also use the same model class to pass the FullName value instead of using ViewBag. I would suggest the below model class

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; }
}

then change your controller to this

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);
}

and change your view to below

@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>

Upvotes: 1

Related Questions