モデルクラスのstudent.csを持つMVC Webアプリを開発しています
プロパティ名、アドレス、ポイントがあり、ポイントは整数です。ビューで私は使用しようとしています:
<%=Html.TextBoxFor(model => model.points) %>
コンパイラはそれを受け入れることができません。整数に Html.TextBoxFor を使用する方法は?
モデルクラスのstudent.csを持つMVC Webアプリを開発しています
プロパティ名、アドレス、ポイントがあり、ポイントは整数です。ビューで私は使用しようとしています:
<%=Html.TextBoxFor(model => model.points) %>
コンパイラはそれを受け入れることができません。整数に Html.TextBoxFor を使用する方法は?
最小値が 1 の数値のみが必要な場合は、次のように実行できます。@Html.TextBoxFor(model => model.Id, new {@type = "number", @min = "1"})
新しい 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);
}
}
整数を文字列にキャストする必要があります
@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)