1

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; }
}
6
  • You can only send primitive datatypes with Url.Action. I would put all primitive types required for your object into your Url.Action and serialize the object in your controller. Commented Mar 22, 2017 at 12:42
  • 1
    why not send ID or a primary key to the controller and handle in controller to send the model back. Commented Mar 22, 2017 at 12:43
  • So is it not possible to pass an object through http url ? Commented Mar 22, 2017 at 12:44
  • it is possible but what are you trying to send can you give example? Commented Mar 22, 2017 at 12:46
  • You could attach a jquery click event to your row and then send the row data (item) as a part of the jquery post to your controller. Will be much simpler Commented Mar 22, 2017 at 12:56

1 Answer 1

2

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

        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.