编辑器不允许输入十进制值
EditorFor not allowing to enter decimal value
我的模型有小数字段
[DisplayFormat(DataFormatString = "{0:0.00}", ApplyFormatInEditMode = true)]
public decimal Sum { get; set; }
我想创建输入字段,用户可以在其中输入带两位小数的小数值
[HttpGet]
public IActionResult TransferForm()
{
string serialized = HttpContext.Session.GetString("CurrentUser") as string;
UserData user = JsonConvert.DeserializeObject<UserData>(serialized);
Transfer newTransfer = new Transfer();
newTransfer.Date = DateTime.Now;
if (user.Balance + 2000 < 0)
{
TempData["MaxValue"] = 0;
}
else
{
TempData["MaxValue"] = user.Balance + 2000;
}
newTransfer.Sender = user.FirstName + " " + user.Surname;
ViewData.Model = newTransfer;
return View();
}
<div class="form-group">
@Html.LabelFor(model => model.Sum, "Kwota", htmlAttributes: new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.EditorFor(model => model.Sum, new { htmlAttributes = new { @class = "form-control", @placeholder = "Kwota", @type = "number", @min = "0.00", @max = TempData["MaxValue"], @step=0.01 } })
</div>
</div>
问题是没有step属性,我不能输入十进制值,只有int是正确的。我以某种方式强制该字段接受带有 step 属性的十进制值,但是使用此属性,当用户按下提交按钮时,将读取 null。
此表单的 GET 端点
我从会话中读取 UserData 对象到
这是页面:
@model Solution.Models.Transfer
@using (Html.BeginForm("TransferForm", "Home", FormMethod.Post))
{
<div class="form-horizontal">
<h2>Zrób przelew</h2>
<!--Pole na tytuł-->
<div class="form-group">
@Html.LabelFor(model => model.Title, "Tytuł", htmlAttributes: new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control", @placeholder = "Tytuł" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Sum, "Kwota", htmlAttributes: new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.EditorFor(model => model.Sum, new { htmlAttributes = new { @class = "form-control", @placeholder = "Kwota", @type = "number", @min = "0.00", @max = TempData["MaxValue"], @step=0.01 } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Date, "Data", htmlAttributes: new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Sender, "Nadawca", htmlAttributes: new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.EditorFor(model => model.Sender, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Recipient, "Odbiorca", htmlAttributes: new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.EditorFor(model => model.Recipient, new { htmlAttributes = new { @class = "form-control", @placeholder = "Odbiorca" } })
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" value="Potwierdź" class="btn btn-dark" />
</div>
</div>
Transfer.cs
public class Transfer
{
public string Title { get; set; }
[DisplayFormat(DataFormatString = "{0:0.00}", ApplyFormatInEditMode = true)]
public decimal Sum { get; set; }
public DateTime Date { get; set; }
public string Recipient { get; set; }
public string Sender { get; set; }
public Transfer()
{
}
public Transfer(string title, decimal sum, DateTime date, string recipient, string sender)
{
Title = title;
Sum = sum;
Date = date;
Recipient = recipient;
Sender = sender;
}
}
您通过 @model
声明(强类型视图)声明了您的视图数据模型。因此,而不是 ViewData.Model = newTransfer;
将数据模型传递给 TransferForm
视图,如下所示:
public IActionResult TransferForm()
{
...
return View(newTransfer);
}
TransferForm.cshtml
的片段:
...
@using (Html.BeginForm("TransferForm", "Home", FormMethod.Post))
{
@* Two lines below added to support the data binding for the Post... *@
@Html.HiddenFor(m => Model.Sender)
@Html.HiddenFor(m => Model.Date)
...
以及截图:
我的模型有小数字段
[DisplayFormat(DataFormatString = "{0:0.00}", ApplyFormatInEditMode = true)]
public decimal Sum { get; set; }
我想创建输入字段,用户可以在其中输入带两位小数的小数值
[HttpGet]
public IActionResult TransferForm()
{
string serialized = HttpContext.Session.GetString("CurrentUser") as string;
UserData user = JsonConvert.DeserializeObject<UserData>(serialized);
Transfer newTransfer = new Transfer();
newTransfer.Date = DateTime.Now;
if (user.Balance + 2000 < 0)
{
TempData["MaxValue"] = 0;
}
else
{
TempData["MaxValue"] = user.Balance + 2000;
}
newTransfer.Sender = user.FirstName + " " + user.Surname;
ViewData.Model = newTransfer;
return View();
}
<div class="form-group">
@Html.LabelFor(model => model.Sum, "Kwota", htmlAttributes: new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.EditorFor(model => model.Sum, new { htmlAttributes = new { @class = "form-control", @placeholder = "Kwota", @type = "number", @min = "0.00", @max = TempData["MaxValue"], @step=0.01 } })
</div>
</div>
问题是没有step属性,我不能输入十进制值,只有int是正确的。我以某种方式强制该字段接受带有 step 属性的十进制值,但是使用此属性,当用户按下提交按钮时,将读取 null。
此表单的 GET 端点 我从会话中读取 UserData 对象到
这是页面:
@model Solution.Models.Transfer
@using (Html.BeginForm("TransferForm", "Home", FormMethod.Post))
{
<div class="form-horizontal">
<h2>Zrób przelew</h2>
<!--Pole na tytuł-->
<div class="form-group">
@Html.LabelFor(model => model.Title, "Tytuł", htmlAttributes: new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control", @placeholder = "Tytuł" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Sum, "Kwota", htmlAttributes: new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.EditorFor(model => model.Sum, new { htmlAttributes = new { @class = "form-control", @placeholder = "Kwota", @type = "number", @min = "0.00", @max = TempData["MaxValue"], @step=0.01 } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Date, "Data", htmlAttributes: new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Sender, "Nadawca", htmlAttributes: new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.EditorFor(model => model.Sender, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Recipient, "Odbiorca", htmlAttributes: new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.EditorFor(model => model.Recipient, new { htmlAttributes = new { @class = "form-control", @placeholder = "Odbiorca" } })
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" value="Potwierdź" class="btn btn-dark" />
</div>
</div>
Transfer.cs
public class Transfer
{
public string Title { get; set; }
[DisplayFormat(DataFormatString = "{0:0.00}", ApplyFormatInEditMode = true)]
public decimal Sum { get; set; }
public DateTime Date { get; set; }
public string Recipient { get; set; }
public string Sender { get; set; }
public Transfer()
{
}
public Transfer(string title, decimal sum, DateTime date, string recipient, string sender)
{
Title = title;
Sum = sum;
Date = date;
Recipient = recipient;
Sender = sender;
}
}
您通过 @model
声明(强类型视图)声明了您的视图数据模型。因此,而不是 ViewData.Model = newTransfer;
将数据模型传递给 TransferForm
视图,如下所示:
public IActionResult TransferForm()
{
...
return View(newTransfer);
}
TransferForm.cshtml
的片段:
...
@using (Html.BeginForm("TransferForm", "Home", FormMethod.Post))
{
@* Two lines below added to support the data binding for the Post... *@
@Html.HiddenFor(m => Model.Sender)
@Html.HiddenFor(m => Model.Date)
...
以及截图: