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, such as this) into the model.
eg.
<input type="Tags" type="text" id="Tags" name="Tags"/>
...
public class Question
{
public string Title { get; set; }
public string Body { get; set; }
public LazyList<string> Tags { get; set; }
}
....
var question = new Question();
this.UpdateModel(question, new [] { "Title", "Body", "Tags" });
the Tags property does get instantiated, but it contains only one item, which is the entire data that was entered into the Tags input field. If i want to have a single item in the List (based upon Splitting the string via space) .. what's the best practice do handle this, please?
cheers!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With