我过去一直通过应用以下数据注释来使用编辑器模板:
[UIHint("SomeTemplate")]
View 模型:
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; }
}
如果我想使用特定的日期选择器控件而不是常规日期选择器控件,可以按如下方式实现。
示例:
@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>
对于我的 ID 字段,我想使用 jQuery 自动完成组件。
问题:
如何将附加参数传递到 LabSampleTypeID
和 LabSampleDetailID
的 UIHint
部分 View ? (因为我希望有一个自动完成的编辑器模板,以 URL 和属性名称为例)
我认为我的自动完成编辑器模板/部分应该是什么样子:
$(".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]
}
}));
}
})
}
});
最佳答案
您可以使用AdditionalMetadata
属性:
[UIHint("DateTime")]
[AdditionalMetadata("foo", "bar")]
public DateTime Date { get; set; }
在模板中:
@ViewData.ModelMetadata.AdditionalValues["foo"]
所以如果你想传递一个网址:
[UIHint("DateTime")]
[AdditionalMetadata("controller", "somecontroller")]
[AdditionalMetadata("action", "someaction")]
[AdditionalMetadata("property", "someproperty")]
public DateTime Date { get; set; }
并在您的模板中:
@{
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 - ASP.NET MVC 编辑器-带参数的模板/UIHint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9681107/