0

I am coding a MVC5 internet application and I am trying to pass an object from a View to a ActionResult.

The parameter of the ActionResult is an AssetItem called assetItem.

Here is the function definition:

public ActionResult DeleteAssetFile(AssetItem assetItem)

Here is my View code:

<ul style="list-style-type: none; padding:0;">
    @foreach (var item in Model.assetItems)
    {
        <li>
            <img src="@item.blobItem.Uri.ToString()" alt="item" width="100" height="100" />
            @Html.ActionLink("Delete", "DeleteAssetFile", new { assetItem = item })
        </li>
    }
</ul>

The assetItem in the DeleteAssetFile ActionResult is null.

Can I please have some help with this?

Thanks in advance

Simon
  • 7,991
  • 21
  • 83
  • 163
  • You would need to create a query string parameter for each property of `assetItem` in order to bind. It would be better to just pass the unique ID of the `assetItem` to the method. –  Sep 21 '14 at 12:17
  • The assetItem is only created in the ActionResult for this one View. Can you explain what you mean by create a query string parameter for each property of assetItem? – Simon Sep 22 '14 at 02:48
  • 1
    Not sure why you would not just pass the ID of the `assetItem` (surely that's all that needed to delete it), but if you wanted to pass the whole object then you would need something like (assuming `assetItem` contains properies `ID`, `Name` and `OtherProperty`) `@Html.ActionLink("Delete", "DeleteAssetFile", new { ID = item.ID, Name = item.Name, OtherProperty = item.OtherProperty })` which will create `/YourController/DeleteAssetFile?ID=1&Name=ABC&OtherProperty=XYZ`. Note there are restrictions on the length of query strings. –  Sep 22 '14 at 03:30

0 Answers0