ASP.NET MVC主编模板/ UIHint带参数 [英] ASP.NET MVC Editor-Templates/UIHint with parameters

查看:185
本文介绍了ASP.NET MVC主编模板/ UIHint带参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在过去像这样使用编辑器的模板已经通过应用以下数据注释:

  [UIHint(SomeTemplate)]

视图模型:

 公共类MicroViewModel
 {
    公共IEnumerable的< LabMicro>万分之一{搞定;组; }    [UIHint(日期时间)
    公众的DateTime日期{搞定;组; }    公众诠释CaseNo {搞定;组; }    [UIHint(SampleTypes)]
    公众诠释LabSampleTypeID {搞定;组; }    [UIHint(SampleDetails)]
    公众诠释LabSampleDetailID {搞定;组; }
 }

如果我想用,而不是常规的一个特定日期选取器控件,它可以如下实现。

示例:

  @model日期时间?
@ Html.TextBox(,的String.Format({0:YYYY-MM-DD},Model.HasValue?
        型号:DateTime.Today),新{@class =DP,风格=WIDTH:100px的})<脚本类型=文/ JavaScript的>
    $(文件)。就绪(函数(){
        $(DP)。日期选择器({
            changeMonth:真实,
            changeYear:真实,
            DATEFORMAT:'YY-MM-DD
        });
    });
< / SCRIPT>

有关我的ID字段,我想利用jQuery的自动完成组件。

问:

我怎么会去传递额外的参数为 LabSampleTypeID UIHint 局部视图和 LabSampleDetailID ?的(为ID喜欢有一个自动完成主编的模板,将采取URL和属性名为例)

我觉得我的自动完成编辑,模板/部分应该是这样的:

  $(自动)。自动完成({
    来源:函数(请求,响应){
        $阿贾克斯({
            网址:'[#URL_TO_USE]',
            数据类型:JSON
            数据:{
                过滤:request.term
            },
            成功:功能(数据){
                响应($。图(EVAL(数据),功能(项目){
                    返回{
                        标签:项目[#PROPERTY_TO_USE]
                    }
                }));
            }
        })
    }
});


解决方案

您可以使用 AdditionalMetadata 属性:

  [UIHint(日期时间)
[AdditionalMetadata(富,酒吧)]
公众的DateTime日期{搞定;组; }

和在模板:

  @ ViewData.ModelMetadata.AdditionalValues​​ [富]

所以,如果你想传递一个网址:

  [UIHint(日期时间)
[AdditionalMetadata(控制器,somecontroller)]
[AdditionalMetadata(行动,someaction)]
[AdditionalMetadata(财产,someproperty)]
公众的DateTime日期{搞定;组; }

和在你的模板:

  @ {
    VAR值= ViewData.ModelMetadata.AdditionalValues​​;
}<脚本类型=文/ JavaScript的>
$('自动')。自动完成({
    来源:函数(请求,响应){
        $阿贾克斯({
            网址:'@ Url.Action((字符串)值[行动](字符串)值[控制器]),
            数据类型:JSON
            数据:{
                过滤:request.term
            },
            成功:功能(数据){
                响应(
                    $ .MAP(EVAL(数据),功能(项目){
                        返回{
                            标签:项目['@值[属性]']
                        }
                    })
                );
            }
        });
    }
});
< / SCRIPT>

I've been using Editor-Templates in the past like this, by applying the following data annotation:

[UIHint("SomeTemplate")]

ViewModel:

 public class MicroViewModel
 {
    public IEnumerable<LabMicro> Micros { get; set; }

    [UIHint("DateTime")]
    public DateTime Date { get; set; }

    public int CaseNo { get; set; }

    [UIHint("SampleTypes")]
    public int LabSampleTypeID { get; set; }

    [UIHint("SampleDetails")]
    public int LabSampleDetailID { get; set; }
 }

If I wanted to use a specific date picker control as opposed to the regular one, it can be implemented as follows.

Example:

@model DateTime?    
@Html.TextBox("",  String.Format("{0:yyyy-MM-dd}", Model.HasValue ? 
        Model : DateTime.Today), new { @class = "dp", style="width:100px" })

<script type="text/javascript">    
    $(document).ready(function () {    
        $(".dp").datepicker({    
            changeMonth: true,    
            changeYear: true,
            dateFormat: 'yy-mm-dd'    
        });    
    });      
</script>  

For my ID fields, I'd like to make use of the jQuery auto-complete component.

Question:

How would I go about passing additional parameters to the UIHint partial view for LabSampleTypeID and LabSampleDetailID? (As Id like to have an auto-complete Editor-Template that'll take a URL and property names for example)

What I think my auto-complete Editor-Template/Partial should look like:

$(".auto").autocomplete({
    source: function(request, response) {
        $.ajax({
            url: '[#URL_TO_USE]',
            dataType: "json",
            data: {
                filter: request.term
            },
            success: function(data) {
                response($.map(eval(data), function(item) {
                    return {
                        label: item.[#PROPERTY_TO_USE]
                    }
                }));
            }
        })
    }
});

解决方案

You could use the AdditionalMetadata attribute:

[UIHint("DateTime")]
[AdditionalMetadata("foo", "bar")]
public DateTime Date { get; set; }

and in the template:

@ViewData.ModelMetadata.AdditionalValues["foo"]

so if you wanted to pass an url:

[UIHint("DateTime")]
[AdditionalMetadata("controller", "somecontroller")]
[AdditionalMetadata("action", "someaction")]
[AdditionalMetadata("property", "someproperty")]
public DateTime Date { get; set; }

and in your template:

@{
    var values = ViewData.ModelMetadata.AdditionalValues;
}

<script type="text/javascript">
$('.auto').autocomplete({
    source: function (request, response) {
        $.ajax({
            url: '@Url.Action((string)values["action"], (string)values["controller"])',
            dataType: "json",
            data: {
                filter: request.term
            },
            success: function (data) {
                response(
                    $.map(eval(data), function (item) {
                        return {
                            label: item['@values["property"]']
                        }
                    })
                );
            }
        });
    }                    
});
</script>

这篇关于ASP.NET MVC主编模板/ UIHint带参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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