[英]FormCollection MVC5 not passing decimal to controller
i keep getting errors when trying to post an <input type="number">
from my view to my controller using FormCollection
. 尝试使用
FormCollection
将<input type="number">
从我的视图发布到我的控制器时,我一直收到错误消息。 the database type for expenseBackup is a decimal(8,2)
. feeBackup的数据库类型为
decimal(8,2)
。 I keep getting "Cannot impliticity convert string to decimal?".
我不断收到
"Cannot impliticity convert string to decimal?".
Then when i try expenseBackup = Int32.Parse(formValues["expenseBackup"])
, i get "Input string was not in correct format"
. 然后,当我尝试
expenseBackup = Int32.Parse(formValues["expenseBackup"])
,我得到"Input string was not in correct format"
。 i don't want to have to convert anything in the controller i don't understand why it won't just pass as a decimal through the FormCollection. 我不想在控制器中进行任何转换,我不明白为什么它不会仅仅作为小数通过FormCollection传递。
Controller 控制者
[HttpPost]
public ActionResult Create(FormCollection formValues)
{
var data = new usr_ebillingClientDatabase()
{
client = formValues["client"], //is a string from form
expenseBackup = formValues["expenseBackup"] //is a decimal from form
};
dataContext.table1.InsertOnSubmit(data);
try
{
dataContext.SubmitChanges();
return RedirectToAction("Index");
}
catch (Exception e)
{
return RedirectToAction("Index");
}
}
View 视图
<div class="form-group">
<div class="col-md-10">
@Html.EditorFor(model => model.expenseBackup, new { htmlAttributes = new { @class = "form-control" , , @type="number", @step=".01" } })
</div>
</div>
when you're reading the field from formValues["expenseBackup"]
it's being read as a string. 当您从
formValues["expenseBackup"]
读取字段时, formValues["expenseBackup"]
作为字符串读取。 Convert it to a decimal using Convert.ToDecimal()
. 使用
Convert.ToDecimal()
将其转换为十进制。
expenseBackup = Convert.ToDecimal(formValues["expenseBackup"] ?? 0m);
FormCollection
is a key-value pair collection ( NameValueCollection
) which returns values as string based from provided key, which is also a string. FormCollection
是一个键值对集合( NameValueCollection
),它根据提供的键(也是一个字符串)以字符串形式返回值。 If you're in doubt which number format applied by numeric input during submit, use combination of decimal.TryParse()
and if-condition with string.IsNullOrEmpty()
to check null/empty string value: 如果您
decimal.TryParse()
在提交过程中数字输入采用哪种数字格式,请结合使用decimal.TryParse()
和if-condition与string.IsNullOrEmpty()
来检查空/空字符串值:
decimal expense;
if (!string.IsNullOrEmpty(formValues["expenseBackup"])
&& decimal.TryParse(formValues["expenseBackup"], out expense))
{
var data = new usr_ebillingClientDatabase()
{
client = formValues["client"],
expenseBackup = expense
};
// insert to database
}
else
{
// unable to parse numeric value, do something else
}
If you're sure that numeric representation passed in FormCollection
uses certain decimal separator other than default, use NumberFormatInfo
when parsing with decimal.Parse()
/ decimal.TryParse()
: 如果您确定在
FormCollection
传递的数字表示形式使用默认值以外的某些小数点分隔符,则在使用decimal.Parse()
/ decimal.TryParse()
解析时,请使用NumberFormatInfo
:
var numberFormat = new System.Globalization.NumberFormatInfo() { NumberDecimalSeparator = "," };
var data = new usr_ebillingClientDatabase()
{
client = formValues["client"],
expenseBackup = decimal.Parse(formValues["expenseBackup"], numberFormat);
};
However, I recommend using strongly-typed viewmodel over FormCollection
because you're using EditorFor
, and it will pass property values directly to controller when viewmodel name is included as action parameter. 但是,我建议在
FormCollection
使用强类型的viewmodel,因为您使用的是EditorFor
,当将viewmodel名称作为动作参数包含时,它将直接将属性值传递给控制器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.