我在jQuery Datatables中实现了一个简单的教程,但我不确定如何使用我在数据视图模型中创建的所有数据注释。我该怎么做呢?阿贾克斯之前,我想有(用剃刀),在我看来是这样的:在AJAX jQuery数据表中使用ASP.NET MVC中的数据注释
<tbody>
@foreach (CallableNoteViewModel vm in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => vm.UnderlyingAsset) </td>
<td>@Html.DisplayFor(modelItem => vm.PayoutCurrency)</td>
<td>@Html.DisplayFor(modelItem => vm.Term)</td>
<td>@Html.DisplayFor(modelItem => vm.AutoCallLevel)</td>
<td>@Html.DisplayFor(modelItem => vm.Frequency)</td>
<td>@Html.DisplayFor(modelItem => vm.Barrier)</td>
<td>@Html.DisplayFor(modelItem => vm.CouponBarrier)</td>
<td>@Html.DisplayFor(modelItem => vm.AutoCallableStart)</td>
<td>@Html.DisplayFor(modelItem => vm.UpsideParticipation)</td>
<td>@Html.DisplayFor(modelItem => vm.Fee)</td>
<td>@Html.DisplayFor(modelItem => vm.Coupon)</td>
<td>@Html.DisplayFor(modelItem => vm.AsOfDate)</td>
</tr>
}
</tbody>
现在,当然,我的观点是这样的,有一个空的机构。 :
<table id="ajax_ci_table" class="table table-striped table-bordered table-responsive" style="white-space: nowrap">
<thead>
<tr>
<th>@Html.DisplayNameFor(model => model.UnderlyingAsset)</th>
<th>@Html.DisplayNameFor(model => model.PayoutCurrency)</th>
<th>@Html.DisplayNameFor(model => model.Term)</th>
<th>@Html.DisplayNameFor(model => model.AutoCallLevel)</th>
<th>@Html.DisplayNameFor(model => model.Frequency)</th>
<th>@Html.DisplayNameFor(model => model.Barrier)</th>
<th>@Html.DisplayNameFor(model => model.CouponBarrier)</th>
<th>@Html.DisplayNameFor(model => model.AutoCallableStart)</th>
<th>@Html.DisplayNameFor(model => model.UpsideParticipation)</th>
<th>@Html.DisplayNameFor(model => model.Fee)</th>
<th>@Html.DisplayNameFor(model => model.Coupon)</th>
<th>@Html.DisplayNameFor(model => model.AsOfDate)</th>
</tr>
</thead>
<tbody>
</tbody>
这是由我的AjaxController返回JSON数据填充:
IEnumerable<string[]> stringdata = from c in data
select new[]
{
c.UnderlyingAsset,
c.PayoutCurrency,
c.Term.ToString(),
c.AutoCallLevel.ToString(CultureInfo.InvariantCulture),
c.Frequency.ToString(),
c.Barrier.ToString(CultureInfo.InvariantCulture),
c.CouponBarrier.ToString(CultureInfo.InvariantCulture),
c.AutoCallableStart.ToString(),
c.UpsideParticipation.ToString(CultureInfo.InvariantCulture),
c.Fee.ToString(CultureInfo.InvariantCulture),
c.Coupon.ToString(CultureInfo.InvariantCulture),
c.AsOfDate.ToString(CultureInfo.InvariantCulture)
};
但现在我的数据注释显然不工作,我对我的格式化视图模型使用。这样的东西:
[Display(Name = "PayFreq")]
[UIHint("FrequencyAndTerm")]
public int Frequency
{
get { return _callableNote.Frequency; }
}
如何格式化我的资料或使用现有的数据注释不直接返回格式化数据在我的控制器(在我上次的代码块)?我觉得在控制器中返回格式化数据是错误的。这不是视图的工作吗?
感谢您的帮助!
简短的答案是你不能(没有返回格式化数据)。你向客户端和客户返回的json对剃刀(c#服务器端代码)一无所知,所以它不能被执行 –