MenuItem Action and passing the Model from the view

2 posts, 0 answers
  1. Lee
    Lee avatar
    10 posts
    Member since:
    Jan 2013

    Posted 15 Jul 2015 Link to this post

    I currently am using buttons on my view for the menu.  So along the top of my view I have 


        .HtmlAttributes(new { type = "submit", value = "AddTeacher", name = "Command", title = "Add Teacher" })


    Now I would like to replace these buttons with a menu.  However I can't seem to figure out how to pass the model within my routevalues in the call to the action.  So my menu item looks like:

         .Items(items =>
               .Items(c =>
                        .Text("Add Teacher")
                        .Action("EditMySchool", "School", new { vm=this.Model, Command="AddTeacher" });

     So the problem is that when the call is made to my action on my controller, the parameter vm is null.  So how do I set vm equal to the model on the view so that I can pass my viewmodel back to my controller?


    Thanks in advance,




  2. Daniel
    Daniel avatar
    2231 posts

    Posted 17 Jul 2015 Link to this post

    Hello Lee,

    The model cannot be passed directly because MVC serializes the route values by calling their ToString method which will result in the name of the type when called on an object. The model fields should be passed separately. The simplest option to achieve this is to create a RouteValueDictionary from the object and add the additional parameters to the dictionary:
    .Items(c =>
        var values = new RouteValueDictionary(Model);
        values["Command"] = "AddTeacher";
            .Text("Add Teacher")
            .Action("EditMySchool", "School", values);

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top