0

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".

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.

3
  • please provide more information Commented May 25, 2013 at 10:30
  • It is a solution for finding dinner places. So the solution allows administratort to create dinner with "Longitude", "Latitude", and "Description", etc. Everything works well except "Longitude" and "Latitude". I defined them as Decimal(18,10) in my database. And LINQ for my database model. When I tried to input these two in my form with some data like "9.432", it showed my error about"the inputed number is invalid". Commented May 25, 2013 at 12:03
  • Are you including any client-side code to format the textbox? like javascript used to format your decimal number like this: "485,388.50" ? Commented May 25, 2013 at 12:30

1 Answer 1

1

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

[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

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.

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

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

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

Dear, thanks for sharing the knowledge with me. But I use LINQ to SQL. No code for database.
Dear if you share your code where you are getting, we can help u. Only overall scenario can't help any of stackoverflow users.
Dear I already put my code. I hope it can make you guys understanding my solution. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.