ModelBinding asp.net MVC 列表

发布于 2024-11-09 10:40:45 字数 220 浏览 0 评论 0原文

我有以下课程:

public class Movie
{
   string Name get; set;
   string Director get;  set;
   IList<String> Tags get; set;
}

如何绑定标签属性?到一个简单的输入文本,以逗号分隔。但仅适用于我正在编码的控制器,不适用于孔应用程序。 谢谢

I have the following class:

public class Movie
{
   string Name get; set;
   string Director get;  set;
   IList<String> Tags get; set;
}

How do I bind the tags properties? to a simple imput text, separated by commas. But only to the controller I'am codding, no for the hole application.
Thanks

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

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

发布评论

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

评论(1

养猫人 2024-11-16 10:40:45

您可以从编写自定义模型绑定器开始:

public class MovieModelBinder : DefaultModelBinder
{
    protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor, object value)
    {
        if (propertyDescriptor.Name == "Tags")
        {
            var values = bindingContext.ValueProvider.GetValue(propertyDescriptor.Name);
            if (values != null)
            {
                value = values.AttemptedValue.Split(',');
            }
        }
        base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
    }
}

然后将其应用到应该接收输入的特定控制器操作:

public ActionResult Index([ModelBinder(typeof(MovieModelBinder))] Movie movie)
{
    // The movie model will be correctly bound here => do some processing
}

现在,当您发送以下 GET 请求时:

/index?tags=tag1,tag2,tag3&name=somename&director=somedirector

或使用 HTML

< /code>:

@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(x => x.Name)
        @Html.EditorFor(x => x.Name)
    </div>
    <div>
        @Html.LabelFor(x => x.Director)
        @Html.EditorFor(x => x.Director)
    </div>
    <div>
        @Html.LabelFor(x => x.Tags)
        @Html.TextBoxFor(x => x.Tags)
    </div>
    <input type="submit" value="OK" />
}

Movie 模型应正确绑定在控制器操作中,并且仅在此控制器操作内。

You could start with writing a custom model binder:

public class MovieModelBinder : DefaultModelBinder
{
    protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor, object value)
    {
        if (propertyDescriptor.Name == "Tags")
        {
            var values = bindingContext.ValueProvider.GetValue(propertyDescriptor.Name);
            if (values != null)
            {
                value = values.AttemptedValue.Split(',');
            }
        }
        base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
    }
}

and then applying it to a particular controller action which is supposed to receive the input:

public ActionResult Index([ModelBinder(typeof(MovieModelBinder))] Movie movie)
{
    // The movie model will be correctly bound here => do some processing
}

Now when you send the following GET request:

/index?tags=tag1,tag2,tag3&name=somename&director=somedirector

Or POST request with an HTML <form>:

@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(x => x.Name)
        @Html.EditorFor(x => x.Name)
    </div>
    <div>
        @Html.LabelFor(x => x.Director)
        @Html.EditorFor(x => x.Director)
    </div>
    <div>
        @Html.LabelFor(x => x.Tags)
        @Html.TextBoxFor(x => x.Tags)
    </div>
    <input type="submit" value="OK" />
}

The Movie model should be bound correctly in the controller action and only inside this controller action.

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