Question

In my view i am using editortemplate as below:

edit.cshtml

@model NetasCrm.Models.CRM_OPP_DETAILS 

<table class="table table-hover">
<thead>
    <tr>
        <th>Çözüm</th>
        <th>Üretici</th>
        <th>Tarih</th>
        <th>Tutar</th>
        <th>Sil</th>
    </tr>
</thead>
    <tbody>
        @Html.EditorFor(model => model.CRM_SOLUTION_DISTRIBUTION, new { Solutions = ViewBag.Solutions, Vendors = ViewBag.Vendors })
        @Html.HiddenFor(model => model.ID)         
    </tbody>
</table>

It is working but i am trying to do something(adding where clause.) as below to create an editor template for some of the items in my model.

@Html.EditorFor(model => model.CRM_SOLUTION_DISTRIBUTION.Where(p=>p.AMOUNT != 0), new { Solutions = ViewBag.Solutions, Vendors = ViewBag.Vendors })

I am getting the below error:

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

Was it helpful?

Solution 2

Html.XxxFor expects a lambda expression that selects a property from the model. This is used to identify the property of the model which will be edited.

What you're trying to achieve is probably what you can do with an editor template.

Interesting articles on editor templates and their implementation:

In the second article you can see that if you have a custom class with the data you want to edit, you can create a template which is automatically used for your editor if you give it the right name and save it in the right place. You can make a class with a single field and include it in your model instead of the original property. This also allows to edit several properties at once (declaring a class with those properties)

Othe option would be a custom html helper:

OTHER TIPS

It's not how @Html.EditorFor() should be used, error message clear states about that.

To render what you want you may either use Html.Partial(), or create separate property in your model and move Where to it's getter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top