0

I want to allow multiple input-formats for a input field. In my case it is a Longitude field. In the DB I set it to decimal(18,10). It expects to recieve a comma-separated-value e.g. 16,2345678 but Google Maps or some Users are using a dot e.g. 16.2345678 I dont want to return an error but simply be glad and transform it to the expected db-format.

I tried to do it in my MetaData Validation Class (using Entity Framework)

public partial class Job

    /*[Bind(Exclude = "ID")]*/
    public class JobMetaData
    {
        public object Longitude
        {
            get { return this.Longitude; }
            set { this.Longitude = value; /* Seems that this point isnt reached*/ }
        }

but unfortunatly the setter is not called and the ViewState.isValid returns simply false.

[HttpPost]
    public ActionResult Edit(Job model)
    {

        // parse the long-lat if needed here??

        try
        {
            if (!ModelState.IsValid)
            {

Where should I try to parse the value to allow both values (comma-separated and dot-separated) transform them and safe it.

I have the same issue for another field: I would like the user to enter 4 or 4€, in case just delete the €-sign and save it as a number to db.

Thanks for your help.

2
  • 1
    your get;set; looks like recursion to me Commented Feb 25, 2011 at 20:03
  • seems like. But I'm note sure. Its a partial class and in the "other part" of this partial class there is also a property Longitude with get/set (the EntityFramework Class). It seems that that this get/set isnt actually called at all, therefore I cannot do any preprocessing.. Commented Feb 27, 2011 at 19:06

2 Answers 2

1

If you have same issue for another field.
as a best practise -
1] use ViewModel "JobViewModel"

2] so that If required you can create Custom validation attributes for your other properties

3] In Edit Post accept ViewModel. [HttpPost] public ActionResult Edit(Job model) {

    // parse the long-lat if needed here??

    try
    {
        if (!ModelState.IsValid)
        {

4] and if Modellstate isValid perform required parsing and then save it to database.

EDIT

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(JobViewModel jobviewModel)
    {
    try
        {
                if (!ModelState.IsValid)
        {
            jobviewModel.Longitude.replace(".",",")
            -- save DB logic here

        }

    }

    }

public class JobViewModel 
{
    public string Longitude{ get; set; }
}


For GET methods need to use use Automapper. or 

    jobviewModel.Longitude = model.Longitude
Sign up to request clarification or add additional context in comments.

6 Comments

With custom validation attributes, how is it possible to do any preprocessing? Inside such a custom attibute definition there is a method isValid returning true/false but its not possible to correct a value if necessary - I can return true for validation, but the wrong value will be passed to the db which will raise an error. Did I loose something?
@LukeSolar You are correct. Custom validation attribute will provide you Isvalid method to return true or flase- but using viewmodel is a recommended practise for MVC project and this will help you to modify values as per your requirement and then save modified viewmodel into database.
@swapneel If I have a model job containing Title, Longitude, etc. (generated, Entity Framework), My ViewModel just contains this Model job. Where should I implement the preprocessing after the post? can you post an example?
@swapneel thanks for the link, nice ressource but I could not find the answer to the question above.
|
0

Use the string type and do whatever you want with it.

public partial class Job
{
    public class JobMetaData
    {
        public string Longitude { get; set; }
    }
}

1 Comment

@Oennning Unfortunately it doesnt work. The EF-Model itself is generated from DB containing some basic validation (the type, required). This JobMetaData Class adds other valiation attributes on top of them. I thought it would make sense to add some preprocessing in this place. I admit Im not sure if I got you right?

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.