2

我正在开发 MVC Web 应用程序,有一个模型类student.cs

它具有属性名称、地址、点,其中点是整数。鉴于我正在尝试使用:

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

编译器不能接受它。如何将 Html.TextBoxFor 用于整数?

4

3 回答 3

2

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);
        }


    }
于 2013-04-16T09:08:27.183 回答
2

如果您只想要最小值为 1 的数字,您可以这样做:@Html.TextBoxFor(model => model.Id, new {@type = "number", @min = "1"})

于 2016-04-14T07:06:15.923 回答
1

您需要将整数转换为字符串

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

编辑: 代码应该可以工作。一个非常简单的测试

该模型

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

控制器

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

风景

@Html.TextBoxFor(model => model.Id)
于 2013-04-16T08:14:20.370 回答