简体   繁体   English

在MVC5中绑定数字的空白字段时,显示占位符而不是0

[英]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 ControllersMVC 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 我知道它是因为我已经在模型中传递了数字的空字段,但是我需要一种变通方法,以便在传递空数字时显示占位符,而不是使用javascriptjquery显示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.

相关问题 当日期为空时,如何显示在日期输入字段中设置为“ mm / dd / yyyy”而不是“ 1/1/0001”的占位符? - How do you display placeholder which is set as 'mm/dd/yyyy' instead of “1/1/0001” in date input fields when date is null? 在MVC5中单击href属性时,如何显示带有按钮的对话框? - How to display a dialog box with buttons when a href attribute is clicked in MVC5? MVC5如何在运行功能时显示加载条/旋转圆/加载器 - MVC5 how do I display a loading bar/spinning circle/loader when a function is running 在空白的输入字段中显示占位符文本 - Show placeholder text in empty input fields 当字段为空时,val()返回占位符值而不是IE8,9中的实际值 - val() returns Placeholder value instead of the actual value in IE8,9 when the field is empty 视图和控制器mvc5之间的两种数据绑定 - two way data binding between view and controller mvc5 在MVC5中将通用列表与kickout.js绑定 - Binding a generic list with knockout.js in MVC5 当两个字段为空javascript/jquery时如何显示错误消息? - How to display error message when two fields are empty javascript/jquery? MVC5 Ajax.ActionLink加载新页面而不是替换 - MVC5 Ajax.ActionLink Loads New Page Instead of Replacing 如何在MVC5中的运行时在HTML5 Canvas中显示图像? - How to display image in HTML5 Canvas at runtime in MVC5?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM