asp.net mvc - Using htmlextensions (html.HiddenFor) in own extention -


i'm trying create html extension combines 3 hiddenfor helpers. i've managed far below, @ least compile:

 public static class batchhelper {     public static mvchtmlstring displayhiddensummary<tmodel, tvalue>(this htmlhelper<tmodel> helper, batchsummary batchsummary) tmodel : batchsummary{          var batchid = helper.hiddenfor(model => model.batchid);         var batchtotal = helper.hiddenfor(model => model.batchtotal);         var totalexpected = helper.hiddenfor(model => model.totalexpected);          var result = mvchtmlstring.create(batchid.tostring() + batchtotal.tostring() + totalexpected.tostring());         return result;     } } 

i having problems using though, getting warnings parameters required, make sense, though examples i've looked @ seem recognise extension method , don't require htmlhelper parameter.

thanks!

edit view code follows:

@using gridmvc.html @using gridmvc.sorting @using myproj.web.viewmodels.receipt @model myproj.web.viewmodels.grids.donorsummarygrid  @html.antiforgerytoken()  @if (model.batchsummary != null) {     // these work fine, , i'm trying put in extension.     @html.hiddenfor(model => model.batchsummary.batchid)     @html.hiddenfor(model => model.batchsummary.batchtotal)     @html.hiddenfor(model => model.batchsummary.totalexpected)      // want use extension     @(html.displayhiddensummary(model.batchsummary)) } 

your function should follows. tvalue parameter unnecessary. also, lambdas incorrect.

public static class batchhelper {         public static mvchtmlstring displayhiddensummary<tmodel,tproperty>(this htmlhelper<tmodel> helper, tproperty batchsummary)  tmodel : myviewmodel tproperty: batchsummary  {              var batchid = helper.hiddenfor(a => batchsummary.batchid);             var batchtotal = helper.hiddenfor(a => batchsummary.batchtotal);             var totalexpected = helper.hiddenfor(a => batchsummary.totalexpected);              var result = mvchtmlstring.create(batchid.tostring() + batchtotal.tostring() + totalexpected.tostring());             return result;         } } 

your razor should follows, assuming typed view of type myviewmodel , property named batchsummary:

@(html.displayhiddensummary<myviewmodel, batchsummary>(model.batchsummary)) 

Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -