Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

ASP.NET MVC Editor-Templates/UIHint with parameters

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]
                    }
                }));
            }
        })
    }
});
question from:https://stackoverflow.com/questions/9681107/asp-net-mvc-editor-templates-uihint-with-parameters

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...