Reputation: 417
I have a Model with an item of type object
called myobject
:
public class myModel
{
public long id {get;set;}
public object myobject {get;set;}
}
in my View I use EditorFor
to edit the myobject
with a template:
@model myModel
// form (code omitted)
@Html.EditorFor(model => model.myobject)
<button type="submit">Save</button>
This is the template:
@model myObject
@Html.EditorFor(model => model.myname)
How can I get the myModel.myobject
value in the controller?
I've tried to cast but I get this error:
Unable to cast object of type 'System.Object' to type 'MyProject.Models.myobject'.
Upvotes: 0
Views: 96
Reputation: 36733
The property in the model is of type object
:
public object myobject { get; set; }
Your EditorTemplate is of type myObject
- NOT of type object
:
@model myObject <-- This little thing is responsible telling MVC what editor template to use with which type.
@Html.EditorFor(model => model.myname)
This is why you aren't getting the implementation you're looking for.
As an alternative you can use a named EditorTemplate instead of letting MVC infer what template you want based on the type.
Upvotes: 1