lundi 2 novembre 2015

How to access ViewModel on the controller

I am learning MVC and I am trying to understand how to access the ViewModel at the controller level.

My question is how do I access the ViewModel in MethodTwo?

  • Do I need to send the ViewModel back in the AJAX call?
  • Is there a simple way just to be able to access the View Model in that method?

For example:


public class MyViewModel
    public int MyID { get; set; }
    public string MyString { get; set; }
    public bool MyBool { get; set; }

In the controller I have this:

public ActionResult MethodOne(int myId, string myString, bool myBool)
    MyViewModel mvm = new MyViewModel { MyID = myId, MyString = myString,yBool = myBool };
    return View("MyPrint", mvm);

On my MyPrint page I am doing an Ajax GET (to the same controller) and I want to access that same ViewModel.

public string MethodTwo()
    // How can I access the ViewModel here???
    //var myString = DoStuff(MyViewModel);
    return myString;

Aucun commentaire:

Enregistrer un commentaire