具有复杂数据输入字段的 ASP.NET MVC UpdateModel

发布于 2024-07-09 17:17:22 字数 588 浏览 4 评论 0原文

我如何使用 ASP.NET MVC UpdateModel 执行以下操作? 我正在尝试将空格分隔的文本框数据(与新的 StackOverflow 问题中的 TAGS 文本框完全相同,例如这个)读入模型中。

例如。

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

Tags 属性确实被实例化,但它只包含一项,即输入到 Tags 输入字段的全部数据。 如果我想在列表中包含单个项目(基于通过空格分割字符串)..处理这个问题的最佳实践是什么?

干杯!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃不饱 2024-07-16 17:17:22

您需要做的是将 DefaultValueProvider 扩展为您自己的。 在您的值提供程序中,扩展 GetValue(name) 以拆分标签并加载到您的 LazyList 中。 您还需要更改对 UpdateModel 的调用:

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

我写的 QuestionValueProvider 是:

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

希望这有帮助

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文