简体   繁体   English

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

[英]Using Data Annotations in ASP.NET MVC with AJAX jQuery Datatables

I've implemented AJAX in my jQuery Datatables following a simple tutorial but I'm not sure how to use all the data annotations I created in my data viewmodel. 我已经按照一个简单的教程在jQuery数据表中实现了AJAX,但是我不确定如何使用在数据视图模型中创建的所有数据注释。 How do I do this? 我该怎么做呢? Before Ajax, I would have something like this in my view (using razor): 在使用Ajax之前,我的观点是(使用剃刀):

 <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>

Now, naturally, my view looks like this, with an empty body. 现在,自然地,我的观点看起来像这样,身体空着。 :

<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>

This is populated by my AjaxController which returns JSON Data: 这是由我的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)
                };

But now my Data Annotations obviously don't work that I used on my view model for formatting. 但是现在我的数据注释显然不起作用,而我在视图模型上使用它进行格式化。 Stuff like: 像这样的东西:

    [Display(Name = "PayFreq")]
    [UIHint("FrequencyAndTerm")]
    public int Frequency
    {
        get { return _callableNote.Frequency; }
    }

How do I format my data or use my existing data annotations without directly returning formatting data in my controller (in my second last code block)? 如何格式化数据或使用现有的数据批注而不直接在控制器中返回格式化数据(在我的倒数第二个代码块中)? I feel like it is wrong to return formatted data in the controller. 我觉得在控制器中返回格式化数据是错误的。 Isn't that the view's job? 这不是视图的工作吗?

Thanks for your help! 谢谢你的帮助!

CoolboyJules, CoolboyJules,

I don't think that DisplayFor uses annotations. 我不认为DisplayFor使用注释。 Try using an editor type helper instead of a display. 尝试使用编辑器类型的助手而不是显示。

@Html.TextboxFor(m => m.UnderlyingAsset);

Not sure what your whole view looks like, but here is a quick template. 不确定您的整体视图是什么样子,但这是一个快速模板。 The model has a Boolean IsViewOnly flag 该模型具有布尔值IsViewOnly标志

@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>
}

These helpers look for and add the proper html to your rendered control which should display your attributes. 这些帮助程序会寻找适当的html并将其添加到呈现控件的应显示属性的html中。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM