ASP.NET MVC UpdateModel with a sorta complex data entry field

后端 未结 1 1733
不知归路
不知归路 2021-01-31 23:03

how do i do the following, with an ASP.NET MVC UpdateModel? I\'m trying to read in a space delimeted textbox data (exactly like the TAGS textbox in a new StackOverflow question,

相关标签:
1条回答
  • 2021-01-31 23:48

    What you need to do is extend the DefaultValueProvider into your own. In your value provider extend GetValue(name) to split the tags and load into your LazyList. You will also need to change your call to UpdateModel:

    UpdateModel(q, new[] { "Title", "Body", "Tags" }, 
       new QuestionValueProvider(this.ControllerContext));
    

    The QuestionValueProvider I wrote is:

     public class QuestionValueProvider : DefaultValueProvider
        {
            public QuestionValueProvider(ControllerContext controllerContext)
                : base(controllerContext)
            {
            }
            public override ValueProviderResult GetValue(string name)
            {
                ValueProviderResult value = base.GetValue(name);
                if (name == "Tags")
                {
                    List<string> tags = new List<string>();
                    string[] splits = value.AttemptedValue.Split(' ');
                    foreach (string t in splits)
                        tags.Add(t);
    
                    value = new ValueProviderResult(tags, null, value.Culture); 
                }
                return value;
            }
        }
    

    Hope this helps

    0 讨论(0)
提交回复
热议问题