This is a migrated thread and some comments may be shown as answers.

MenuItem Action and passing the Model from the view

1 Answer 124 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Lee
Top achievements
Rank 1
Lee asked on 15 Jul 2015, 04:19 PM

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

 

@(Html.Kendo().Button()
    .Name("btnAddTeacher")
    .HtmlAttributes(new { type = "submit", value = "AddTeacher", name = "Command", title = "Add Teacher" })
    .ImageUrl(Url.Content("~/Images/add-student-35.png"))
    .Enable(ElementaryProgress.Classes.Security.AllowSchoolEdit)
)

 

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:

@(Html.Kendo().Menu()
     .Name("menu")
     .Items(items =>
     {
         items.Add()
           .Text("Teacher")
           .Items(c =>
           {
                c.Add()
                    .Text("Add Teacher")
                    .ImageUrl("~/Images/add-student-35.png")
                    .Enabled(ElementaryProgress.Classes.Security.AllowSchoolEdit)
                    .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,

Lee

 

 

1 Answer, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 17 Jul 2015, 12:15 PM
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";
 
    c.Add()
        .Text("Add Teacher")
        .ImageUrl("~/Images/add-student-35.png")
        .Enabled(ElementaryProgress.Classes.Security.AllowSchoolEdit)
        .Action("EditMySchool", "School", values);
});


Regards,
Daniel
Telerik
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
Tags
Menu
Asked by
Lee
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Share this question
or