Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7549487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:57:28+00:00 2026-05-30T09:57:28+00:00

I have to write a lot of code that’s going to generate Knockout JS

  • 0

I have to write a lot of code that’s going to generate Knockout JS templates from classes.

Say something like this…

 <li><span>Surname</span> <span data-bind="text: SURNAME"></span></li>

which I’d like to invoke in a razor template with something like…

 @className.DisplayMeFor(c=>c.SURNAME)

or even

 @DisplayMeFor<className>(c=>c.SURNAME)

but I’m really stuck knowing where to start. I clearly need to do some reading up on generics but I thought it would be something like this…

public static class HtmlExtensions
{
    public static MvcHtmlString DisplayMeFor<TModel, TValue>(this TModel htmlHelper,   Expression<Func<TModel, TValue>> expression)
    {
        var s = expression.ToString(); //Clearly need a lot more code here to get name, DisplayName etc
        return MvcHtmlString.Create(s);
    }
}

but that’s not providing an extension to the model (i.e. @vmAppeal.DisplayMeFor(… does not compile.

Any pointers please ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:57:29+00:00Added an answer on May 30, 2026 at 9:57 am

    The solution I came up with was

    public static class Exts
    {
        private const string Input = @"<div><span>{0} : </span><input data-bind='value: {1}'></input></div>";
        private const string Display = @"<div><span>{0} : </span><span data-bind='text: {1}'></span></div>";
        private const string DatePicker = @"<div><span>{0} : </span><input data-bind='datepicker: {1}, datepickerOptions: {{  dateFormat: ""dd/mm/yy""}}' /></div>";
    
        public static MvcHtmlString TemplateFor<TModel>(Expression<Func<TModel, object>> expression, bool edit) where TModel : class  , new()
        {
            var data = new ViewDataDictionary<TModel>();
            var metadata = ModelMetadata.FromLambdaExpression(expression, data);
            var typ = metadata.ModelType;
    
            var s = edit? Input: Display;
            if (typ == typeof(System.DateTime) ||typ == typeof(System.DateTime?))
            {
                s = edit ? DatePicker : Display;
            }
            return MvcHtmlString.Create(string.Format(s, metadata.GetDisplayName(), metadata.PropertyName));
        }
    }
    

    which is called

    @(Exts.TemplateFor<MyClass>(a=>a.MyField,true))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to write a bash script that makes lot of things. I'd like
I find that I write a lot of code within my classes to keep
I have to write something in vbscript that need to use a unique set.
I have certain code that I want to optimize. It looks like this: function
i have to write some code that initialize my data app. This must be
I have some Code that I'd like to share between an iOS and an
The debug screen says that django does not have write access to the db.
I'm writing a simple web app in PHP that needs to have write access
I have to write an applet that brings up a password dialog. The problem
I've been running across a lot of Perl code that breaks long strings up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.