code_junkie

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

Nov 12th, 2011
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. <input type="Tags" type="text" id="Tags" name="Tags"/>
  2.  
  3. ...
  4.  
  5. public class Question
  6. {
  7. public string Title { get; set; }
  8. public string Body { get; set; }
  9. public LazyList<string> Tags { get; set; }
  10. }
  11.  
  12. ....
  13.  
  14. var question = new Question();
  15. this.UpdateModel(question, new [] { "Title", "Body", "Tags" });
  16.  
  17. UpdateModel(q, new[] { "Title", "Body", "Tags" },
  18. new QuestionValueProvider(this.ControllerContext));
  19.  
  20. public class QuestionValueProvider : DefaultValueProvider
  21. {
  22. public QuestionValueProvider(ControllerContext controllerContext)
  23. : base(controllerContext)
  24. {
  25. }
  26. public override ValueProviderResult GetValue(string name)
  27. {
  28. ValueProviderResult value = base.GetValue(name);
  29. if (name == "Tags")
  30. {
  31. List<string> tags = new List<string>();
  32. string[] splits = value.AttemptedValue.Split(' ');
  33. foreach (string t in splits)
  34. tags.Add(t);
  35.  
  36. value = new ValueProviderResult(tags, null, value.Culture);
  37. }
  38. return value;
  39. }
  40. }
Add Comment
Please, Sign In to add comment