sergserg
sergserg

Reputation: 22254

CS0411: The type arguments for method 'System.Web.Mvc.Html.EditorExtensions.EditorFor<>)' cannot be inferred from the usage

I have a model class I created; a simple POCO class:

public class ContactModel
{
    [Required]
    public string Name { get; set; }

    [Required]
    public string Email { get; set; }

    [Required]
    public string Message { get; set; }

    [Required]
    public string Work{ get; set; }        
}

Inside a view, I'd like to call and editor for this model:

<div class="contact-form">
    @Html.EditorFor(new Map.WebUI.Models.ContactModel())
</div>

But I get the error:

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0411: The type arguments for method 'System.Web.Mvc.Html.EditorExtensions.EditorFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Source Error:

How can I invoke an editor for a random class, considering the view is not strongly typed to this object type?

Upvotes: 1

Views: 5632

Answers (1)

Romias
Romias

Reputation: 14133

In your view you set at the top @model Map.WebUI.Models.ContactModel

Then you need to use the EditorFor this way:

@Html.EditorFor(x => x.ContactModel())

the parameter is a Lamda Expresion.

EDIT:

Ok, I don't got that you cannot change it... so... I think you cannot use EditorFor. But what you CAN do is use a PartialView and use:

@Html.Partial("YourContactView", new Map.WebUI.Models.ContactModel())

EDIT 2

You can also use the @Html.Editor(string expression, ViewData data)... that way you can place the Model to pass to the Editor in the ViewData object.

Upvotes: 5

Related Questions