Amsel
Amsel

Reputation: 107

ASP.NET MVC pass an object from table to the controller?

I want to pass a model to the controller after klicking on a row.

this is my code which doesn't work.

<table class="table">
<tr>
    <th>
        <p>Sender</p>
    </th>
    <th>
        <p>Subject</p>
    </th>
    <th>
        <p>Received</p>
    </th>
    <th>
        <p>HasAttachment</p>
    </th>
    <th></th>
</tr>

@foreach (var item in Model.Inbox) {
<tr class="mail-row" onclick="location.href = '@(Html.ActionLink   ("", "MailViewer", "Profile", new { mail = item }, null ))'">
    <td>
        @Html.DisplayFor(modelItem => item.Sender)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Subject)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Received)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.HasAttachment)
    </td>
</tr>
}

</table>

I tried to use Url.Action but i got only the Object Type in the url and not the actual object.

I try to send this object:

public class Mail
{
    public string Sender { get; set; }
    public string Subject { get; set; }
    public DateTime Received { get; set; }
    public bool HasAttachment { get; set; }

    public string content { get; set; }
}

Upvotes: 1

Views: 1070

Answers (1)

Usman
Usman

Reputation: 4693

it is possible with json but you will but you should send id and get the data in action based on that id but with json you can do it like this

My action link will be

@Html.ActionLink("senddata", "MailViewer",new{ @data = Json.Encode(item)  } )

and my action will be something like this

public ActionResult MailViewer(string data)
        {
            var result = System.Web.Helpers.Json.Decode<Mail>(data);

            return View();

        }

Upvotes: 2

Related Questions