[英]Display placeholder instead of 0 when binding empty fields of numbers in MVC5
I am working on edit in MVC 5
with API
Controllers
. 我正在使用
API
Controllers
在MVC 5
进行编辑。 When user presses edit, a model with values fetched from db will be passed. 当用户按下edit时,将传递一个具有从db获取的值的模型。 However when adding a new data, empty model will be passed with following code
但是,当添加新数据时,将使用以下代码传递空模型
var Model = {
Id: '',
Type: ''
}
$.ajax({
url: '@Url.Action("Action", "Control")',
type: 'POST',
data: { model: Model },
success: function (data) {
}
My web controller 我的网页控制器
public ActionResult Add(Model model)
{
return View(model);
}
Now when I try to add the values, all the fields with string
type will show placeholder but integers
will show 0 instead of placeholder. 现在,当我尝试添加值时,所有
string
类型的字段都将显示占位符,但integers
将显示0而不是占位符。 I know its because I have passed empty field for numbers in my model but I need a workaround such that when empty numbers are passed it shows placeholder instead of displaying 0 using javascript
or jquery
我知道它是因为我已经在模型中传递了数字的空字段,但是我需要一种变通方法,以便在传递空数字时显示占位符,而不是使用
javascript
或jquery
显示0
When you want to model binding to avoid 0 as default to integer type , please user null able integer value 当您想对绑定进行建模以避免默认为整数类型为0时,请使用null整数值
public int? Count { get; set; }
This will assign as null to integer property rather than zero when you post model from java script or jquery as black or empty. 当您从Java脚本或jquery中将模型发布为black或empty时,它将为null分配给integer属性,而不是零。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.