首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在AJAX jQuery数据表中使用ASP.NET MVC中的数据注释

在AJAX jQuery数据表中使用ASP.NET MVC中的数据注释
EN

Stack Overflow用户
提问于 2016-09-22 21:22:39
回答 1查看 850关注 0票数 0

遵循一个简单的教程,我已经在我的jQuery Datatables中实现了AJAX,但是我不确定如何使用我在数据视图模型中创建的所有数据注释。我该怎么做呢?在使用Ajax之前,我的视图中会有如下内容(使用razor):

代码语言:javascript
运行
复制
 <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>

现在,很自然地,我的观点是这样的,没有身体。:

代码语言:javascript
运行
复制
<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数据:

代码语言:javascript
运行
复制
 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)
                };

但是现在,我的数据注释显然不能用于我的视图模型的格式化。像这样的东西:

代码语言:javascript
运行
复制
    [Display(Name = "PayFreq")]
    [UIHint("FrequencyAndTerm")]
    public int Frequency
    {
        get { return _callableNote.Frequency; }
    }

如何格式化数据或使用现有的数据注释,而不直接在控制器中返回格式化数据(在我的倒数第二个代码块中)?我觉得在控制器中返回格式化数据是错误的。这不是视图的工作吗?

谢谢你的帮忙!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-22 21:57:12

CoolboyJules,

我不认为DisplayFor使用注解。尝试使用编辑器类型的帮助器而不是显示。

代码语言:javascript
运行
复制
@Html.TextboxFor(m => m.UnderlyingAsset);

不确定您的整个视图是什么样子,但这里有一个快速模板。该模型具有布尔型IsViewOnly标志

代码语言:javascript
运行
复制
@using (Ajax.BeginForm("Edit", "Inventory", null, ajaxOptions, new { id = "frmInventoryEditModal" }))
{
    <!-- modal div -->
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">×</button>
        <h2 style="color: darkorchid Title</h2>
    </div>
    <div id="ErrorSummary">
        @Html.ValidationSummary(true)
    </div>
    <div class="modal-body" id="modal-body">
            <div class="row-fluid DataRow">
            @using (Html.ControlGroupFor(m => m.UnderlyingAsset))
            {
                @Html.LabelFor(m => m.UnderlyingAsset, new { @class = "control-label" })
                <div class="controls">
                    @Html.TextBoxFor(m => m.UnderlyingAsset)
               </div>
            }
        </div>  
        // Continue for the rest of the fields in model         
    </div>
    <div class="modal-footer">
        @if (!Model.IsViewOnly)
        {
            <button id="btnSave" type="button" class="btn btn-primary">Save changes</button>
        }
        <button id="btnCancel" class="btn btn-success" data-dismiss="modal" aria-hidden="true">Cancel</button>
    </div>
}

这些帮助器查找适当的html并将其添加到呈现的控件中,该控件应显示您的属性。

希望这能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39640291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档