0

I want to render a form used to create a new instance of a Model. I tried,

@Html.EditorFor(model => new Person())

But I got the error,

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

I tried,

@Html.EditorForModel("MyNamespace.Person")

but nothing rendered. How do I use Html.EditorFor when I dont have a model instance to pass to it?

1 Answer 1

1

It seems that you're a bit confused about how to use @Html.EditorFor(). First,, your use of the lambda expression doesn't quite make sense, model => new Person(). If you want to create a new instance of a model then there's no need to use a lambda, just new Person() gives you that instance.

Second, you don't need an actual instance to pass into @Html.EditorFor(). The purpose of this method is to produce an html markup including an input field for a property or even multiple properties.

So let's say your Person model had an Age property, then you can create an edit field for that attribute by calling

@Html.EditorFor(model => model.Age)

Now where does model come from? You have to define it in your view, so add this line to the top of your view file so that ASP.net knows which model to grab the Age attribute from,

@model <namespace>.Models.Person

The namespace is usually the name of your project, and if you don't put your models in the Models folder, then replace that with where ever your model is contained in.

Sign up to request clarification or add additional context in comments.

5 Comments

Yep. Im definitely confused! So I should not be using EditorFor for the model itself and I only use it on rendering input fields for properties? If so, how should I be rendering the full form? Is it a best practice to put it in a partial and call with @Html.Partial?
You can use @Html.EditorFor(m => m) to generate all the form controls for your model, but usually you create an EditorTempate for your model to control the output
@StephenMuecke How would that work when there isn't an instance of the model to reference? As I am writing this all out, I'm thinking that maybe I need to redesign my application.
All you need is @model Person at the top of the view as Chris has noted. You do not need to pass a model to the view, but its best practice to do so in your controller, for example return View(new Person()); if you just want to create a new Person
hey @Jeff I would suggest calling @Html.EditorFor() multiple times for the properties you want the user to give input for to generate the full form. And the model to be referenced will come from @model <namespace>.Models.Person in your view. If what I'm saying sounds greek to u and you don't mind some scaffolded/generated code, then try right clicking your action method in your controller class and select Add View and choose the Create template. If you have any more questions feel free to ask.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.