MVC 3 + knockoutjs:使用EditorFor一个布尔值字段时,将数据绑定属性 [英] MVC 3 + knockoutjs: adding the data-bind attribute when using EditorFor for a boolean field

查看:93
本文介绍了MVC 3 + knockoutjs:使用EditorFor一个布尔值字段时,将数据绑定属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 @ Html.EditorFor(型号=> model.IsClient),其中IsClient是一个布尔值,呈现一个下拉列表与不集,是和否作为选项。

Using @Html.EditorFor(model =>model.IsClient), where IsClient is a boolean, renders a drop down list with Not Set, Yes and No as the options.

都很好。

现在我想用knockoutjs与得到的DropDownList,我喜欢,所以我怎么添加使用@ Html.EditorFor数据绑定属性,我需要knockoutjs与此下拉?

Now I want to use knockoutjs with the resulting dropdownlist, that I like, so how do I add the data-bind attribute using @Html.EditorFor, that I need for knockoutjs to work with this drop down?

我曾尝试:

@Html.EditorFor(model => model.IsClient, new Dictionary<string, object> { { "data-bind", "value: Account.IsClient" } })

不过,这种使用对象additionalViewData参数,它不呈现数据绑定属性。这可能是很自然的,因为这个参数可能是无关的HTML属性来呈现的标签。

However, this uses the object additionalViewData parameter, and it doesn't render the data-bind attribute. Which is probably quite natural, as this parameter is probably nothing to do with Html Attributes for the rendered tag.

然而,找不到任何合理的文件,并没有其他重载寻找可能的候选人,因为我想要的东西。

However, can't find any reasonable documentation, and none of the other overloads look likely candidates for what I want.

TIA任何建议。

推荐答案

布拉德·威尔逊<一个href=\"http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html\"相对=nofollow>博客有关显示和编辑模板ASP.NET MVC 2。所以,你可以修改布尔默认模板,并添加你需要的(〜/查看/共享的属性/EditorTemplates/MyTemplate.cshtml

Brad Wilson blogged about display and editor templates in ASP.NET MVC 2. So you could modify the default template for boolean and add the attributes you need (~/Views/Shared/EditorTemplates/MyTemplate.cshtml):

@{
    bool? value = null;
    if (ViewData.Model != null) 
    {
        value = Convert.ToBoolean(ViewData.Model, System.Globalization.CultureInfo.InvariantCulture);
    }

    var triStateValues = new List<SelectListItem> 
    {
        new SelectListItem 
        { 
            Text = "Not Set",
            Value = String.Empty,
            Selected = !value.HasValue 
        },
        new SelectListItem 
        { 
            Text = "True",
            Value = "true",
            Selected = value.HasValue && value.Value 
        },
        new SelectListItem 
        { 
            Text = "False",
            Value = "false",
            Selected = value.HasValue && !value.Value 
        },
    };
}

@if (ViewData.ModelMetadata.IsNullableValueType) 
{
    <!-- TODO: here you can use any attributes you like -->
    @Html.DropDownList(
        "", 
        triStateValues, 
        new { 
            @class = "list-box tri-state", 
            data_bind="value: " + ViewData.TemplateInfo.GetFullHtmlFieldName("") // you could also use ViewData.ModelMetadata.PropertyName if you want to get only the property name and not the entire navigation hierarchy name
        }
    )
} 
else 
{
    @Html.CheckBox("", value ?? false, new { @class = "check-box" })
}

最后:

@Html.EditorFor(model => model.IsClient, "MyTemplate")

或装饰上使用 UIHint 属性您的视图模型IsClient属性:

or decorate the IsClient property on your view model with the UIHint attribute:

[UIHint("MyTemplate")]
public bool? IsClient { get; set; }

和则:

 @Html.EditorFor(x => x.IsClient)

会自动选择自定义编辑模板。

will automatically pick the custom editor template.

这篇关于MVC 3 + knockoutjs:使用EditorFor一个布尔值字段时,将数据绑定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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