有关asp.net mvc中Decimal数据类型的验证错误

时间:2022-06-01 20:49:21

I defined a data type decimal(18,10) for longitute and latitute in my database. But it always said "validation error" when I tried to input and submit my form. I used LINQ to SQL. Is there some validation rules it generated for me otherwise why I can not input these two with something numbers like "2.34".

我为我的数据库中的longitute和latitute定义了一个十进制数据类型(18,10)。但是当我尝试输入并提交表单时,它总是说“验证错误”。我使用LINQ to SQL。它是否为我生成了一些验证规则,否则为什么我不能用“2.34”之类的数字输入这两个。

Thanks in advance

提前致谢

namespace Nerddinner.Models
{
    interface IDinnerRepository
    {
        IQueryable<Dinner> FindAllDinners();
        Dinner GetDinner(int id);
        void AddDinner(Dinner dinner);
        void UpdateDinner(Dinner dinner);
        void DeleteDinner(Dinner dinner);
    }
}


namespace Nerddinner.Models
{
    public class sqlDinnerRepository: IDinnerRepository
    {
        dbDataContext db;
        public sqlDinnerRepository()
        {
            db = new dbDataContext();
        }

        public IQueryable<Dinner> FindAllDinners()
        {
            return db.Dinners;
        }

        public Dinner GetDinner(int id)
        {
            return db.Dinners.SingleOrDefault(x => x.DinnerID == id);
        }

        public void AddDinner(Dinner dinner)
        {
            db.Dinners.InsertOnSubmit(dinner);
        }

        public void UpdateDinner(Dinner dinner)
        {
            db.SubmitChanges();
        }

        public void DeleteDinner(Dinner dinner)
        {
            db.Dinners.DeleteOnSubmit(dinner);
        }
    }
}


namespace Nerddinner.Controllers
{
    public class DinnerController : Controller
    {
        IDinnerRepository _repository;
        public DinnerController()
        {
            _repository = new sqlDinnerRepository();
        }

        public DinnerController(IDinnerRepository repository)
        {
            _repository = repository;
        }

        //
        // GET: /Dinner/

        public ActionResult Index()
        {
            var dinners = _repository.FindAllDinners();
            return View(dinners);
        }

        //
        // GET: /Dinner/Details/5

        public ActionResult Details(int id)
        {
            var dinner = _repository.GetDinner(id);
            return View(dinner);
        }

        //
        // GET: /Dinner/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Dinner/Create

        [HttpPost]
        public ActionResult Create(Dinner dinner)
        {
            try
            {
                // TODO: Add insert logic here
                _repository.AddDinner(dinner);
                _repository.UpdateDinner(dinner);

                return RedirectToAction("Index");
            }
            catch
            {
                return View(dinner);
            }
        }

        //
        // GET: /Dinner/Edit/5

        public ActionResult Edit(int id)
        {
            var dinner = _repository.GetDinner(id);
            return View(dinner);
        }

        //
        // POST: /Dinner/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {

            var db = new dbDataContext();
            var dinner = db.Dinners.SingleOrDefault(x => x.DinnerID == id);
            try
            {
                // TODO: Add update logic here
                UpdateModel(dinner, collection.ToValueProvider());
                _repository.UpdateDinner(dinner);
                return RedirectToAction("Index");
            }
            catch
            {
                return View(dinner);
            }
        }

        //
        // POST: /Dinner/Delete/5

        [HttpPost]
        public ActionResult Delete(int id)
        {
            var db = new dbDataContext();
            var dinner = db.Dinners.SingleOrDefault(x => x.DinnerID == id);
            try
            {
                // TODO: Add delete logic here
                _repository.DeleteDinner(dinner);
                _repository.UpdateDinner(dinner);
                return RedirectToAction("Index");
            }
            catch
            {
                return View(dinner);
            }
        }
    }
}

Thanks for helping me.

谢谢你的帮助。

1 个解决方案

#1


1  

In ASP.NET MVC, You can use the DisplayFormatAttribute on your model property:

在ASP.NET MVC中,您可以在模型属性上使用DisplayFormatAttribute:

[DisplayFormat(DataFormatString = "{0:0.##}")]
public decimal decimalNumber { get; set; }

The above will output a number with up to 2 decimal places. For more information visit: Custom Numeric Format Strings and Standard Numeric Format Strings

以上将输出一个最多2位小数的数字。有关更多信息,请访问:自定义数字格式字符串和标准数字格式字符串

IN SQL SERVER: *decimal(m,a)*: m is the number of total digits your decimal can have, while a is the max number of decimal points you can have.

在SQL SERVER中:* decimal(m,a)*:m是小数可以包含的总位数,而a是您可以拥有的最大小数位数。

so if you put PI into a Decimal(18,0) it will be recorded as 3

因此,如果将PI放入十进制(18,0),它将被记录为3

if you put PI into a decimal(18,2) it will be recorded as 3.14

如果你将PI放入小数(18,2),它将被记录为3.14

if you put PI into Decimal(18,10) be recorded as 3.1415926535

如果你将PI放入十进制(18,10),则记录为3.1415926535

I think my answer will help you. Correct me if I am wrong.

我想我的回答会对你有所帮助。如果我错了,请纠正我。

#1


1  

In ASP.NET MVC, You can use the DisplayFormatAttribute on your model property:

在ASP.NET MVC中,您可以在模型属性上使用DisplayFormatAttribute:

[DisplayFormat(DataFormatString = "{0:0.##}")]
public decimal decimalNumber { get; set; }

The above will output a number with up to 2 decimal places. For more information visit: Custom Numeric Format Strings and Standard Numeric Format Strings

以上将输出一个最多2位小数的数字。有关更多信息,请访问:自定义数字格式字符串和标准数字格式字符串

IN SQL SERVER: *decimal(m,a)*: m is the number of total digits your decimal can have, while a is the max number of decimal points you can have.

在SQL SERVER中:* decimal(m,a)*:m是小数可以包含的总位数,而a是您可以拥有的最大小数位数。

so if you put PI into a Decimal(18,0) it will be recorded as 3

因此,如果将PI放入十进制(18,0),它将被记录为3

if you put PI into a decimal(18,2) it will be recorded as 3.14

如果你将PI放入小数(18,2),它将被记录为3.14

if you put PI into Decimal(18,10) be recorded as 3.1415926535

如果你将PI放入十进制(18,10),则记录为3.1415926535

I think my answer will help you. Correct me if I am wrong.

我想我的回答会对你有所帮助。如果我错了,请纠正我。