Okay so I'm iterating over models with a Razor foreach, and I'd like to send the model to jQuery. I can send individual parameters as seen in this answer, but not the full object.
Example:
<table>
@foreach (var contact in Model)
{
<tr>
<td>
<a href="#" onclick="myFunction('@contact.Name')">@contact.Name</a>
</td>
<td>
<a href="#" onclick="myFunction('@contact.Company')">@contact.Company.Name</a>
</td>
</tr>
}
</table>
and
<script type="text/javascript">
function myFunction(contact) {
console.log(contact);
};
</script>
Here, sending @contact.Name
will send the value in Name
to the function as expected, but @contact.Company
(which is a Company object, rather than a string) will send the string value ProjectName.Models.Company
. Is it possible to do what I'm trying to do?
Here's an example of one of the actual models I'm using (note that every model I use, like this one, also has another object type as a field).
public class Note
{
public long NoteId { get; set; }
public string Content { get; set; }
public DateTime CreatedOn { get; set; }
public User CreatedBy { get; set; }
}