MVC Binder Binding Problem

I have a text box, in my opinion that I want to get the value as a list of strings.

As an example, if someone enters: tag1, tag2, tag3 ... gets a list of 3 elements.

I made a custom mediator, but im still getting a row instead of List from a column.

This is what I did:

This is my model:

public class BaseItem
{
    [Required]
    [StringLength(100)]
    public string Name
    {
       get;
       set;
    }
    public IList<string> RelatedTags
    {
       get;
       set;
    }

}

My typed view with the model above:

<% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary("Please complete in a right way the fields below.") %>

        <fieldset>
            <legend>Fields</legend>
            <div class="editor-field">
                <%: Html.LabelFor(e => e.Name)%>
                <%: Html.TextBoxFor(e => e.Name)%>
                <%: Html.ValidationMessageFor(e => e.Name)%>
            </div>
            <div class="editor-field">
                <%: Html.LabelFor(e => e.RelatedTags)%>
                <%: Html.TextBoxFor(e => e.RelatedTags)%>
                <%: Html.ValidationMessageFor(e => e.RelatedTags)%>
            </div>
            <p>
                <input type="submit" />
            </p>
        </fieldset>

    <% } %>

My custom mediation device:

public class TagModelBinder:DefaultModelBinder
{
    protected override object GetPropertyValue(
        ControllerContext controllerContext, 
        ModelBindingContext bindingContext, 
        System.ComponentModel.PropertyDescriptor propertyDescriptor, 
        IModelBinder propertyBinder)
    {
       object value = base.GetPropertyValue(
                          controllerContext, 
                          bindingContext, 
                          propertyDescriptor, 
                          propertyBinder);
       object retVal = value;
       if (propertyDescriptor.Name == "RelatedTags")
       {                 
           retVal = bindingContext.ValueProvider.GetValue("RelatedTags")
                        .RawValue.ToString().Split(',').ToList<string>();
       }
       return retVal;
    }
}

I registered my own connecting device in the Global.asax.cs file:

ModelBinders.Binders.Add(typeof(string), new TagModelBinder());

The problem I am facing is that it never enters the GetPropertyValue method of my custom binder.

Of course, I have nothing to lose. Could you give me a hand?

+3
1

typeof IList, , .

ModelBinders.Binders.Add(typeof(IList<string>), new TagModelBinder());

, .

+3

Source: https://habr.com/ru/post/1760043/


All Articles