Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have combed the Internet for anything slightly related to this (and other sites) for several days trying to figure this out. Please help (-:

What I have is a form with a lot of textboxes on it. Some are required, some are not. I have 2 boxes that are for Social Security Numbers and they have to match, named "TaxID" and "SSNAgain".

I didn't name the first one, as I would have named it SSN, but someone else did that. I have already written the code to check the match in both boxes so I had to make them both required and they have to have 9 digits in them. Problem is, I only need to submit the first one "TaxID" and not the "SSNAgain" box to the database.

Nothing I try will work, it just keeps either refreshing the page and coming back to the "SSNAgain" box with it highlighted in pink (probably because there's no field in the database for it), or it sends me to the generic error page provided by ASP.NET MVC when we set this up several years ago.

Also, I had to change the ssn boxes to
ASP.NET
@Html.PasswordFor
boxes because our company just got bought out the other day and these guys wanted the SSN boxes covered with the little dots like password boxes are. Below are some things I have been trying but to no avail. Thanks in advance to ANYONE that can help me with this matter.

NOTHING I try will work. I just need to submit the page to the database and NOT submit the contents from SSNAgain. But the clients do have to put in their SSN number and it has to match what's in TaxID. At this point, I'm just grasping at straws here.

What I have tried:

//Please know I didn't write all this code, this was written by our back-end guy.
public class IndividualController : Controller
{
    private DataForm18Entities db = new DataForm18Entities();// he named it that because we built this in 2018

    public ActionResult Index()
    {
        //ModelState["SSNAgain"].Errors.Clear();//tried this to clear the validation so it can submit, but didn't work.
        System.Threading.Thread.Sleep(2000);//just to pause it a little for the animated loader to do its thing.
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Index([Bind(Include = "RowID,TaxID,FirstName,LastName,BirthDate,blah,blah,blah)] DataForm_Individual dataForm_Individual)
    {
        //ModelState["SSNAgain"].Errors.Clear();//tried putting this here...
        if (ModelState.IsValid)
        {  //ModelState["SSNAgain"].Errors.Clear();//probably tried putting this here...
            try
            {
                db.DataForm_Individual.Add(dataForm_Individual);
                db.SaveChanges();
                //some other code to send success email
                return View("Success");
            }
            catch (Exception e)
            {
                //code to send error info to email
                return View("Error");
            }
        }

        return View(dataForm_Individual);
    }
}

//Here's my annotations:
[Required(ErrorMessage = "SSN is required")]
[RegularExpression(@"[0-9]*\.?[0-9]+", ErrorMessage = "SSN must be numbers only.")]
[StringLength(9, MinimumLength = 9, ErrorMessage = "Invalid SSN Length")]
[DataType(DataType.Password)]
[Display(Name = "SSN", Description = "Please enter your Social Security Number without spaces or dashes")]
public string TaxID { get; set; }

[Required(ErrorMessage = "SSN is required")]
[RegularExpression(@"[0-9]*\.?[0-9]+", ErrorMessage = "SSN must be numbers only.")]
[StringLength(9, MinimumLength = 9, ErrorMessage = "Invalid SSN Length")]
[DataType(DataType.Password)]
[Display(Name = "Confirm SSN", Description = "Please enter your Social Security Number again without spaces or dashes")]
public string SSNAgain { get; set; }
C#



And below is my JavaScript. I even tried doing it that way.
JavaScript
$(function () {
$("#IndvForm").submit(function () {//the ID of my form...
      //$("#SSNAgain").removeAttr("data-val-required");//I tried this...
      if ($(this).valid()) {
          //$("#SSNAgain").rules("remove"); //I tried to remove all the rules this way also
          $("#loading").fadeIn();//this is for the spin.js plugin I use to have a loading animation upon submit.
            var opts = {
                lines: 12, // The number of lines to draw
                length: 7, // The length of each line
                width: 4, // The line thickness
                radius: 10, // The radius of the inner circle
                color: '#000', // #rgb or #rrggbb
                speed: 1, // Rounds per second
                trail: 60, // Afterglow percentage
                shadow: false, // Whether to render a shadow
                hwaccel: false // Whether to use hardware acceleration
            };
            var target = document.getElementById('loading');
            var spinner = new Spinner(opts).spin(target);
            return true;
        }
    });
});
Posted
Updated 6-Nov-21 12:22pm

1 solution

It seems you are using the same data object to pass datas to the view (your HTML page), back to the controller, AND to the database. That's the wrong way to do this.

You should have models (classes) separate for your database work and your views. The database model classes are only responsible for getting data from and passing data back to the database.

Your View model classes would be responsible for passing data to and getting data back from the view code (HTML/Javascript.)

Why is this? Your views have different data requirements than your database does. Your view may need extra data that it needs to fill in other controls (like comboboxes) or render other things on screen. The database doesn't need any of that.
 
Share this answer
 
Comments
millitheKidd 7-Nov-21 15:03pm    
Hello Dave, thank you for replying. Ok, so I snagged a screenshot of the project. This was set up by our back-end/Database guy and I (just being a front-end guy) don't really understand everything about MVC. I'm still trying to wrap my head around all this MVC stuff because I used to do just regular old ASP.Net development back in the day. (-: So here's a link to the image:
https://postimg.cc/MX4ZsF8J
I don't really know if I could get him to change anything if it's set up wrong because he and I work different schedules and I remote in most of the time now anyway and he is super busy with other stuff all the time since he's our full-time DBA. The site has been working good, its just that when I tried to make these changes (such as adding the new "SSNAgain" box) for our new company, that when things went south. LOL.
Dave Kreskowiak 7-Nov-21 19:50pm    
The empty Models folder is a clue to a major design problem.

You're using data classes to fill both the database role and the view roll.

MVC stands for Model, View, Controller. The Models are classes that hold data the view needs to render. That is NOT necessarily the same data a database would use!

Controllers are the interface between your views, moving you from one view to the next, and between the views and the database. If your view models return new data from the view, the controllers transfer that data into the required database classes to get that information into the database. Controllers also take the data in the data classes coming back from your database code and copies any required data into Model classes to be passed to the Views.

If you're passing database classes to the Views, you're doing it wrong. That may work in simple apps, but in more complex app, it's going to bite you in the ass, like what you're going through now.
millitheKidd 7-Nov-21 23:20pm    
Ok, well thank you for answering back Dave. Yeah I always thought it was little weird the way he had this set up. But with my limited knowledge of MVC, I just kind of shrugged it off. (-:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900