ASP.NET MVC 4 Passing an Object Variable Using ActionLink

I have an ASP.NET MVC 4 application. My application has a razor view that works with a list as a model.

@model List<MeetingLog.Models.UserModel> @{ Layout = null; } . . . 

I repeat the Model variable as follows:

 @foreach (var item in Model) { <tr> <td> @item.Name </td> <td> @item.Surname </td> <td> @Html.ActionLink("Click", "SavePerson", "Meeting", new {model = item, type = "coordinator"}, null) @Html.ActionLink("Click", "SavePerson", "Meeting", new {model = item, type = "participant"}, null) </td> </tr> } 

I need to pass two variables to a SavePerson action with valid references. The first is the current UserModel, and the second is a string variable named type. But in my action, the first parameter appears as null. But the string parameter is doing the right thing. How can i achieve this?

+6
source share
3 answers

I use ajax calls for this

 $('.btnSave').on('click', function(){ $.ajax({ url: '@(Url.Action("SavePerson", "Meeting"))', type: 'post', data: { Value1: 'Value1', Value2: 'Value2' }, success: function (result) { alert('Save Successful'); } }); }); 

and place the call by pressing the button or click the link if you want, href = # I hope this helps.

+4
source

You cannot pass instances of complex types through querystring. This is the way to use it:

 @Html.ActionLink("Click", "SavePerson", "Meeting", new {x = item.x, y = item.y, ..., type = "participant"}, null) 
+4
source

Actually, this is a little strange when you encode the passed values ​​(new {}) what you need to do is pass it as the new object you create so that it ends:

 @Html.ActionLink("Link Name", "LinkActionTarget", new Object{model = item, type ='Coordinator'} 

Where Object is the name of your object, and the model and type are attributes of this object

+1
source

Source: https://habr.com/ru/post/955907/


All Articles