c# - Using Data Annotations in ASP.NET MVC with AJAX jQuery Datatables -


i've implemented ajax in jquery datatables following simple tutorial i'm not sure how use data annotations created in data viewmodel. how do this? before ajax, have in view (using razor):

 <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, view looks this, 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 populated ajaxcontroller returns json data:

 ienumerable<string[]> stringdata = 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 data annotations don't work used on view model formatting. stuff like:

    [display(name = "payfreq")]     [uihint("frequencyandterm")]     public int frequency     {         { return _callablenote.frequency; }     } 

how format data or use existing data annotations without directly returning formatting data in controller (in second last code block)? feel wrong return formatted data in controller. isn't view's job?

thanks help!

coolboyjules,

i don't think displayfor uses annotations. try using editor type helper instead of display.

@html.textboxfor(m => m.underlyingasset); 

not sure whole view looks like, here quick template. model has boolean isviewonly flag

@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 rest of 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 , add proper html rendered control should display attributes.

hope helps.


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -