Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Hi Team,

I have a Model on my View, i want to pass that Model to the @Html.ActionLink, i have tried by some google articles, found not fruitful. Within my model i have two another model for ex. lets consider below is my model structure-

Public class TestModel

{

  public Test1Model test1{get;set;}

public Test2Model test2{get;set;}

}

Now, i want to pass TestModel to the @Html.ActionLink and want to access as a parameter to the action of controller.

Please suggest if someone have a nice approach on his.

Thanks.
Posted

1 solution

You can't, in short.

An ActionLink helper renders as an anchor tag (hyperlink). This link, when clicked, performs a GET against the specified controller action, meaning that you can pass parameters to it via the query string (html attributes object within the helper), but you cannot post the model to your controller via an ActionLink. For example:

HTML
@Html.ActionLink("My Link", "MyControllerAction", new { id = Model.Id })


This would submit a GET request to your controller like so:

C#
public async Task<Actionresult> MyControllerAction(int id)
{
    // Do stuff with the id
}


If you want to post a complete model to a controller, use a form with a submit button. This will submit your model back to the controller. You can style the submit button to look like a link.

Alternatively, use the passed in id property to load the model from a data store.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900