2

I have a controller that passes the viewmodel to a view, and in the view it creates the table correctly.

Then in the same page of the table I have a link that redirects the user to another page.

This new page needs to keep the viewmodel of the previous view. How could I do it?

In the view, in the table page this link that should redirect to an action of EanMatch controller:

@Html.ActionLink("Inserimento manuale", "Index", "EanMatch",  null, new {Vm = Model})

The model is this:

public List<DDTViewModel> DDTList { get; set; }

In the EanMatchController and Index action I have:

[HttpGet]
public ActionResult Index(DDTListViewModel Vm)
{
    ....
}

I can't understand why it doesn't work. DDTListViewModel Vm in Index actions takes 'null' value, meanwhile I have seen with debug that Model got the list of data.

How can I pass the viewmodel to the action argument correctly?

Any doubt, ask !

8
  • Is it .net core or .net framework? Commented Dec 5, 2019 at 16:08
  • it is .netframework Commented Dec 5, 2019 at 16:10
  • What is DDTListViewModel ? Is it smth that safety store on the client-side? Commented Dec 5, 2019 at 16:11
  • @staff614 you are setting the ViewModel inside a property inside an anonymous object. This is why you are not getting the result you want. Commented Dec 5, 2019 at 16:12
  • @BasilKosovan DDTListViewModel contains the list of a viewModel public List<DDTViewModel> DDTList { get; set; } Commented Dec 5, 2019 at 16:22

5 Answers 5

1

For your particular case, You'll have to serialize your model as a JSON string, and send that to your controller to turn into an object.

Here's your ActionLink:

@Html.ActionLink("Inserimento manuale", "Index", "EanMatch",  new { jsonModel= Json.Encode(Model.DDTListViewModel) }, null)

And your Controller method would look like:

public ActionResult Index(string jsonModel)
{
  var serializer= new DataContractJsonSerializer(typeof(Model.DDTListViewModel));
  var yourmodel=  (DDTListViewModel)serializer.ReadObject(GenerateStreamFromString(jsonModel));
}
Sign up to request clarification or add additional context in comments.

Comments

1
@Html.ActionLink("LinkName", "Index", "EanMatchController", new ViewModel { Id = 1, Name = "Sth", Description = "Hello" }, null);

You set the object as property to an anonymous object. This is why your object is not passed on the controller.

It is like having a wrapper object around your object like below.

public class Sth{
 public ViewModel { get; set; }
}

So C# does not recognize this as your object you need to somehow create it inside razor and send it back to the controller. The code above works. Try a simple example.

2 Comments

it gives error to 'new viewModel'. however, i tried to put new { id = 1 } and it passes the data to the action, it just doesn't work with the list of viewmodel
Pass your own view model. I just posted as an example. View model is an object with 3 properties.
0

if you controller name is EanMatch, then the next issue is that I think you are passing the model in where it expects htmlattributes. So instead of null, new {Vm = Model}; switch them.

@Html.ActionLink("Inserimento manuale", "Index", "EanMatch", new {Vm = Model} ,null )

2 Comments

Hit f12 and see what's being send post/get.
it posts as query string ?Capacity=16&Count=11 which I can't explain actually
0

You don't need to create the anonymous object, just pass your view model:

@Html.ActionLink("Inserimento manuale", "Index", "EanMatch", null, Model)

The framework will convert your model to a query string that will be bound by your controller.

Comments

0

Try this:

@Html.ActionLink("Inserimento manuale", "Index", "EanMatch",  null,  Model)

OR: Each request to the server has its own context(thread) and doesn't know anything about others. If you need to share a model or another data between a few different requests to the server you can use temp data or session. Inside of the first action, you need to save this model:

Session["myModel"] = Model;

and read in another:

var val = Session["myModel"];

1 Comment

Where actually should "Session["myModel"] = Model;" go? it is between a View to Controller, not action to action

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.