0

what i am doing is simple. based on an object show only the properties i want to. Now having an instantiated object with values. the problem i encountered were a few. if i use an EditorFor to give it the value it will throw the following error.
Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
I tried to fix it but anything i tried wasn't working, the only thing i didn't try is a custom helper. so i decided to see if i can use Editor and this time it doesn't get the values.
I decided to use TextBox this time the values are loaded but when i submit the model view object is null.
Below is the code i use in the view the only change is in the HtmHelper.Editor/Textbox/EditorFor

@foreach (var item in Model.AttributesList) //List of PropertyInfo
    {
        <div class="form-group">
            @Html.Label(item.Name, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.Editor("Test", item.GetValue(Model.object))
            </div>
        </div>
    }

For the editor part i replaced it as follow

@Html.Editor("Test", item.GetValue(Model.entityType))
@Html.TextBox("Test", item.GetValue(Model.entityType).ToString())
@Html.EditorFor(m => item.GetValue(Model.entityType).ToString())
@Html.EditorFor(m => item.GetValue(m.entityType))


public class CustomAttributeClass: Attribute
{
    public bool is_modifiable { get; set; } //english?

    public CustomAttributeClass(bool is_modifiable )
    {
        this.is_modifiable = is_modifiable ;
    }
}

and then in my entity i simply call it [CustomAttributeClass(false)] and i already have a method that fills that list with the entity properties that are not defined as that class

  • You cannot use a `foreach` loop to generate form controls in a collection –  May 20 '16 at 02:28
  • What is it that you actually want to do? What is the purpose of this? –  May 20 '16 at 02:30
  • this basically shows only the attributes that can be modified/updated. i have a customattribute class and that's what the list is for. – I_Tried_Thats_Why_Im_Here May 20 '16 at 02:33
  • What is your _customattribute class_? You should have a view model containing only the properties you need (filtered in the GET method) and then just loop through that (using a `for` loop) to generate the form controls. –  May 20 '16 at 02:37
  • Sorry, but its not making sense. Not only can you not use a `foreach` loop, but you cannot use a method (in your case `.GetValue()`) in a `HtmlHelper`. Just create a view model representing what you want to edit. –  May 20 '16 at 02:57
  • the problem is i have props like ID that can't be modified so that wont be part of the property any idea for that part? – I_Tried_Thats_Why_Im_Here May 20 '16 at 02:58
  • Without seeing more code, its impossible to help, by if a property cannot be modified, then why are you not just using a hidden input or a readonly textbox? –  May 20 '16 at 03:01
  • i could provide more code but i don't know what is missing. as for the other part of the question it's because i am having it generic. just give it the entity and it will get the modifiable attributes and show them. – I_Tried_Thats_Why_Im_Here May 20 '16 at 03:05
  • i tried the get filter on get of an instance but it overflows (exception stackoverflow) because it takes too much time. i understand when you say i can't use the `foreach` so i guess i would've went with the custom model view if i could but i use too many entities. can you provide an example with that `for` how-to you mentioned please? – I_Tried_Thats_Why_Im_Here May 20 '16 at 03:30

0 Answers0