0

I want to generate a MVC-editor with the function

System.Web.Mvc.Html.EditorExtensions.EditorFor<TModel, TValue>(
       this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, 
       object htmlAttributes = null)

The problem is the

Expression<Func<TModel, TValue>> expression

because I have only the name of the property. I'm sure the property exist and I can get the actual instance of the Model.

How can I create the correct parameter?

Edit: To make it more clear: I have the model, I have the name of the property and want to create the Expression>.

Thanks!

Peter

1
  • 1
    Did you try Html.EditorFor(model=>model.SomeProperty); ? Commented Jun 29, 2018 at 15:17

2 Answers 2

1

If you have your property name as a string, use the Editor extension method rather than EditorFor.

e.g.

@Html.Editor("YourProperty", new { htmlAttributes = new { ..
Sign up to request clarification or add additional context in comments.

Comments

1

The answer of Johnathan Barclay guides me the right way!

Instead of html.EditorFor I now use html.Editor.

Here an extraction from the code of the generator-class:

 public static MvcHtmlString GenerateStepTwo(this HtmlHelper<InputModell> html, InputModell modell)
    {
     ...
     foreach (var actField in fieldsSorted)
        {
        ...
        editor = html.Editor(fieldname, new { htmlAttributes = new { @class = "form-control", style = "max-width:200px;min-width:100px" } });

        all.AppendLine(AddOpenImageFunc(editor, fieldname));
...
}
return all;

Thanks! Peter

Comments

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.