How to use html.textboxfor for numbers in MVC

12,634

Solution 1

Create a new ASP.NET MVC project and use only the code you presented:

you will see that it works.

View

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Models.Student>" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        <%= Html.TextBoxFor(model => model.repPoints) %>
    </div>
</body>
</html>

Model

public class Student 
{ 
public Student() { } //constructor :) 

public Student(int ID, int repPoints) 

{ this.ID = ID; this.repPoints = repPoints; } 

public int ID { get; set; } 

public int repPoints { get; set; } }

Controller

 public class TestController : Controller
    {
        public ActionResult Index()
        {
            Student student = new Student(10, 20);

            return View(student);
        }

        public ActionResult UpdateStudent(Student student)
        {
            //access the DB here

            return View("Index",student);
        }


    }

Solution 2

If you only want numbers with minimum value 1 you can do it like this: @Html.TextBoxFor(model => model.Id, new {@type = "number", @min = "1"})

Solution 3

You need to cast the integer to a string

@Html.TextBoxFor(model => model.points.ToString())

Edit: The code should work. A very simple test

The model

public class Product
{
    public int Id { get; set; }
}

The controller

 public ActionResult Index()
 {              
    var model = new Product {Id = 10};
    return View(model);
 }

The view

@Html.TextBoxFor(model => model.Id)
Share:
12,634
Samer_Azar
Author by

Samer_Azar

I work as a Software Developer since 2012, I have experience in: Transact-SQL, C#.NET, ASP.NET web forms, Winforms, Crystal Reports, Windows Services, Web Services, web APIs, and basic Web design(Html, Css).

Updated on June 05, 2022

Comments

  • Samer_Azar
    Samer_Azar almost 2 years

    I am developing MVC web app, having a model class student.cs

    which has properties name, address, points where points is an integer. in view I am trying to use:

    <%=Html.TextBoxFor(model => model.points) %>
    

    The compiler cannot accept it. how to use Html.TextBoxFor for integers?

  • Samer_Azar
    Samer_Azar about 11 years
    then it says: Invalid Operation Exception// Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
  • Samer_Azar
    Samer_Azar about 11 years
    System.FormatException: Input string was not in a correct format. and when i tried to convert it, BANGG
  • Dave
    Dave about 11 years
    Stick a watch in Visual Studio and see what the value of model.points is
  • Samer_Azar
    Samer_Azar about 11 years
    in the model class, points is a property, and i used: <%=Html.TextBoxFor(s => Convert.ToString(s.repPoints)) still, the error message: System.InvalidOperationException: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
  • Samer_Azar
    Samer_Azar about 11 years
    this works, but actually i dowanna set ID to 10 nor repPoints to 20, all i want to do is to set them dynamically, so that repPoints value will be taken from view
  • Mathias F
    Mathias F about 11 years
    see the changed controller. you can post the form to UpdateStudent and student will have the values the user entered