ASP.NET MVC的UpdateModel有八九不离十复杂的数据输入字段 [英] ASP.NET MVC UpdateModel with a sorta complex data entry field

查看:121
本文介绍了ASP.NET MVC的UpdateModel有八九不离十复杂的数据输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么做以下,与ASP.NET MVC的UpdateModel?我想在一个空间delimeted文本数据读取(就像一个新的StackOverflow问题的变量文本,如本)到模型中。

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.

如:

<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?

干杯!

推荐答案

您需要做的是延长DefaultValueProvider到自己。在你的价值供应商延长的GetValue(名称)标签和负载分成您LazyList。您还需要您的来电更改的UpdateModel:

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));

我写的QuestionValueProvider是:

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;
        }
    }

希望这有助于

这篇关于ASP.NET MVC的UpdateModel有八九不离十复杂的数据输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆