首页 > 解决方案 > 我如何提交所有必需的文本框,但一个来自表单?

问题描述

我有一个表格,上面有很多文本框。有些是必需的,有些不是。我有 2 个用于社会安全号码的框,它们必须匹配,命名TaxIDSSNAgain.

我没有命名第一个,因为我会命名它SSN,但是其他人这样做了。我已经编写了代码来检查两个框中的匹配,所以我必须让它们都需要,并且它们必须有 9 位数字。问题是,我只需要将第一个TaxID而不是SSNAgain框提交到数据库。

我尝试的任何方法都不起作用,它只是不断刷新页面并返回以SSNAgain粉红色突出显示的框(可能是因为数据库中没有它的字段),或者它将我发送到 ASP 提供的通用错误页面。 NET MVC,当我们几年前设置它时。

另外,我不得不把 ssn 盒子换成盒子,@Html.PasswordFor因为我们公司前几天刚被收购,这些人想要 SSN 盒子上覆盖着像密码盒子一样的小圆点。以下是我一直在尝试但无济于事的一些事情。

//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);
    }
}

我也尝试过使用 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;
            }
        });
    });

我只需要将页面提交到数据库,而不是从 SSNAgain 提交内容。但是客户确实必须输入他们的 SSN 号码,并且它必须与 TaxID 中的内容相匹配。

哦,是的,我差点忘了。这是我的注释:

[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; }

标签: jqueryasp.net-mvcasp.net-mvc-5entity-framework-6

解决方案


推荐阅读