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 6026431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:26:38+00:00 2026-05-23T04:26:38+00:00

In my project I have the requirement where certain fields depending of a certain

  • 0

In my project I have the requirement where certain fields depending of a certain condition should be editable or just readonly.

So I though that doing this for every field was an overkill

@if (Model.CanEdit)
{ 
    @Html.TextBoxFor(model => Model.Foo, new { width = "100px" })
}
else
{ 
    @Html.TextBox(model => Model.Foo, new { width = "100px", @readonly = "readonly"})
}

I decided to use an editor template, but then I realised that the width cannot be fixed, so I was wondering what’s the best way to send parameters to an Editor template? Also it should handle the scenario that width may not be defined and not use the width property at all. I found that ViewData may help with this, but having a code that looks like this makes me feel I’m doing something wrong.

@inherits System.Web.Mvc.WebViewPage<string>
@if (Model.CanEdit)
{ 
    @if(ViewData["width"] == null)
    {
        @Html.TextBox("", Model, new { width = ViewData["width"].ToString() })
    }
    else
    {
        @Html.TextBox("", Model)
    }
}
else
{ 
    @if(ViewData["width"] == null)
    {
        @Html.TextBox("", Model, new { width = ViewData["width"].ToString() , @readonly = "readonly"})
    }
    else
    {
        @Html.TextBox("", Model, new {@readonly = "readonly"})
    }
}

I wonder if there could be a way to create a helper so I could make something like:

@MyTextBoxFor(model => Model.Foo, true) @* true would be or readonly *@
@MyTextBoxFor(model => Model.Foo, true, 100) @* 100 would be the width length *@
  • 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-23T04:26:40+00:00Added an answer on May 23, 2026 at 4:26 am

    kapsi’s answer is great, but if you want to use your helper in a strongly typed view, here’s the basic syntax. It’s a little sloppy, but you can add overloads as you see fit.

    public static class MyHelpers
    {
        public static MvcHtmlString MyTextBoxFor<TModel, TProperty>(
            this HtmlHelper<TModel> htmlHelper, 
            Expression<Func<TModel, TProperty>> expression, 
            bool flagReadonly,
            int? widthProperty) where TModel : class
        {
            MemberExpression memberExpression = expression.Body as MemberExpression;
            string parameterName = memberExpression.Member.Name;
    
            return new MvcHtmlString(
                string.Format("<input id=\"{0}\" name=\"{0}\" {1} {2} />",
                parameterName, // name and if of parameter
                flagReadonly ? "readonly=\"readonly\"" : string.Empty,
                widthProperty.HasValue ? 
                    string.Format("width=\"{0}px\"", (int)widthProperty) : 
                    string.Empty));
        }
    }
    

    Of course, this way, you’re afforded the ability to strongly type your view elements

    @Html.MyTextBoxFor(model => model.Foo, true)
    

    and

    @Html.MyTextBoxFor(model => model.Foo, true, 100)
    

    etc

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In our project we have requirement that, after receiving sms message from third party
In our project we have a requirement that when a set of records are
For a project we have a requirement to create an interfacedefinition that will return
I'm using ASP.NET MVC in a project and I have a requirement like this..
In my ASP.NET MVC project I have a requirement to return certain pages in
We have a requirement in the project that we have to compare two texts
This has happened twice or thrice in one project. The requirement was to have
Visual Studio Setup project: I have to check certain system requirements before my setup
I am using ASP.NET and C# in my project. I have a requirement where
I have a project where the requirements is that a end user will select

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.