format usage in html.textbox in mvc5

reddy reddy 0 Reputation points
2023-04-19T12:12:42.03+00:00

hi, how to use format in @Html.TextBox("name", Model.Name as below User's image

Developer technologies ASP.NET Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2023-04-20T03:28:17.9766667+00:00

    Hi @reddy reddy, string format:A string that is used to format the input. https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.html.inputextensions.textbox?view=aspnet-mvc-5.2 Let me give two examples so that you can understand it more intuitively. If you need the value of the text box to have two decimal places, you can specify the format as "{0:N2}". @Html.TextBox("Name", Model.name, "{0:N2}", new { style = "color:green" }) public int name { get; set; }

     public ActionResult Index()
            {          
                Table1 table1= new Table1();
                table1.name = 1603;       
                return View(table1);           
            }
    

    Before use: User's image

    After use:User's image

    If you need a specific date format: @Html.TextBox("Name", Model.name, "{0:dd/MM/yyyy}", new { style = "color:green" }) public DateTime name { get; set; }

    public ActionResult Index()
            {          
                Table1 table1= new Table1();
                DateTime date1 = new DateTime(2023, 12, 25);
                table1.name =date1;       
                return View(table1);           
            }
    

    Before use: User's image

    After use:User's image

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.