2

モデルクラスのstudent.csを持つMVC Webアプリを開発しています

プロパティ名、アドレス、ポイントがあり、ポイントは整数です。ビューで私は使用しようとしています:

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

コンパイラはそれを受け入れることができません。整数に Html.TextBoxFor を使用する方法は?

4

3 に答える 3

2

最小値が 1 の数値のみが必要な場合は、次のように実行できます。@Html.TextBoxFor(model => model.Id, new {@type = "number", @min = "1"})

于 2016-04-14T07:06:15.923 に答える
2

新しい ASP.NET MVC プロジェクトを作成し、提示したコードのみを使用します。

それが機能することがわかります。

意見

<%@ 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>

モデル

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

コントローラ

 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 に答える
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 に答える