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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:55:58+00:00 2026-06-15T11:55:58+00:00

I have a text box created using @Html.TextBoxFor(m => m.Model1.field1, new { @class =

  • 0

I have a text box created using

@Html.TextBoxFor(m => m.Model1.field1, new { @class = "login-input", @name="Name",  @Value = "test" })

I want to change the default value of this textbox from “text” to a value stored in a model field. How do i set a model field as the the value attribute? Say the Name of the model to be called is Model2 and the attribute is field2. How do I set the value of the textbox to field2?

  • 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-06-15T11:56:00+00:00Added an answer on June 15, 2026 at 11:56 am

    You must first write an Extension Method like this:

    public class ObjectExtensions
    {
        public static string Item<TItem, TMember>(this TItem obj, Expression<Func<TItem, TMember>> expression)
        {
            if (expression.Body is MemberExpression)
            {
                return ((MemberExpression)(expression.Body)).Member.Name;
            }
            if (expression.Body is UnaryExpression)
            {
                return ((MemberExpression)((UnaryExpression)(expression.Body)).Operand).Member.Name;
            }
            if (expression.Body is ParameterExpression)
            {
                return expression.Body.Type.Name;
            }
            throw new InvalidOperationException();
        }
     }
    

    It will extract the Name of property when you write sth like this: @Html.TextBoxFor(m => m.Model1.field1)

    then you can use it like this:

    Html.TextBoxFor(m => m.Model1.field1, 
      new { @class = "login-input", 
            @name="Name",  
            @value = Model.Item(m => m.Model1.field1) })
    

    If you don’t want to call m => m.Model1.field1 again, you must declare your version of TextBoxFor method which is more complicate, but If you want I can provide you the details.

    This is a sample from my code base on Github:

    public static class HtmlHelperExtensionForEditorForDateTime
    {
    
        public static MvcHtmlString Editor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
        {
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
    
    
            string propname = html.ViewData.Model.Item(expression);
            string incomingValue = null;
            var httpCookie = html.ViewContext.RequestContext.HttpContext.Request.Cookies["lang"];
            if (metadata.Model is DateTime && (httpCookie.IsNull() || httpCookie.Value == Cultures.Persian))
                incomingValue = PersianCalendarUtility.ConvertToPersian(((DateTime)metadata.Model).ToShortDateString());
            if (string.IsNullOrEmpty(incomingValue))
                return html.TextBox(propname, null, new { @class = "datepicker TextField" });
    
            return html.TextBox(propname, incomingValue, new { @class = "datepicker TextField"});
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an form validation using ajax/php. Each text box is validated using
I have text box to enter a phone number. I'm using AJAX MaskedEditExtender and
I am creating a small HTML editor using jQuery. I have created my own
I have a simple ASP.Net MVC View which contains an FCKeditor text box (created
I have an HTML page. I have created a frame using background repeat function.
I have a quick jQuery question: First, my HTML: <div id=taskinput> <form> <input id=taskinput-box
I have created an HTML form with a search box and a drop down
i have created partial view in MVC3. now i want to send text box
I have page with form: @using (Html.BeginForm(Create, Theme, FormMethod.Post, new { id = frm_post,
I have created HTML TABLE from JSON data of PHP using JQuery. After creating

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.