簡體   English   中英

無法在 ASP.NET MVC 中傳遞數據

[英]Unable to pass data in ASP.NET MVC

我無法將數據從我的視圖傳遞到控制器以驗證數據。 ModelState.IsValid是假的。 我不知道我還能在這里做什么。

模型:

public partial class Testing
{
    public int LoginID { get; set; }
    [Required]
    public string EmailID { get; set; }
    [Required]
    public int TaxID { get; set; }
    [Required]
    public string Password { get; set; }

    public string CorporationName { get; set; }

    public static bool IsUserValid()
    {
        HttpContext context = HttpContext.Current;
        if (context.Session["LoginID"] != null)
        {
            return true;
        }
        else
            return false;
    }
}

控制器:

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

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ForgotLoginId(Testing ForgotLIDAuthentication)
{
        if (ModelState.IsValid)
        {
            using (SUPRTestingDBEntities2 db = new SUPRTestingDBEntities2())
            {
                var obj = db.SUPRTesting.Where(a => a.EmailID.Equals(ForgotLIDAuthentication.EmailID) && a.TaxID.Equals(ForgotLIDAuthentication.TaxID)).FirstOrDefault();

                if (obj != null)
                {
                    Session["LoginID"] = obj.EmailID.ToString();
                    Session["EmailID"] = obj.TaxID.ToString();
                    return RedirectToAction("LIDAuthentication");

                }
                else if (obj == null)
                {
                    return View();
                }
            }
        }            

        return View();         
}        

public ActionResult LIDAuthentication()
{
        if (Testing.IsUserValid())
        {
            return View();
        } 
        else
        {
            return RedirectToAction("ForgotLoginID");
        }
}

看法:

@{
    ViewBag.Title = "Corporation Registration";
}

@using (Html.BeginForm("ForgotLoginID", "Corporation", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    if (ViewBag.Message != null)
    {
        <div style="border: 1px solid red">
        @ViewBag.Message
        </div>
    }

    <div style="height: 30px; width: 20%; font-size: large; text-align: center; margin-left: 40%; margin-right: 40%; margin-top: 5%; margin-bottom: 46px; background-color: deepskyblue; color:white" class="auto-style9">
        <b>Forgot Login ID</b>
    </div>

    <div style="height: 475px; width: 50%; text-align: left; margin-left: 25%; margin-right: 25%; margin-top: 10%; margin-bottom: 46px; background-color: #FFFFFF; border-color:black">
        <span style="margin-left:10%"></span>
        <label for="Label1" style="width:20%; color:red; font-weight:normal !important">* Required Entry</label>
        <br />
        <br />
        <br />
        <span style="margin-left:10%"></span>
        <label for="Label5" style="width:20%">Email Address:</label>
        <label for="label6" style="color:red"><b>*</b></label>
        <span style="margin-left:9%"></span>
        @Html.TextAreaFor(a => a.EmailID)
        @Html.ValidationMessageFor(a => a.EmailID)
        <span style="margin-right:9%"></span>

        <br />
        <br />
        <br />
        <span style="margin-left:10%"></span>
        <label for="Label1" style="width:17%">Tax ID:</label>
        <select id="taxid" style="width:10%">
            <option value="fein">FEIN</option>
            <option value="ssn">SSN</option>
        </select>
        <label for="label2" style="color:red"><b>*</b></label>
        <span style="margin-left:20%"></span>
        @Html.TextAreaFor(a => a.Password)
        @Html.ValidationMessageFor(a => a.Password)
        <span style="margin-right:9%"></span>
        <br />
        <br />
        <br />
        <span style="margin-right:9%"></span>
        <label for="label6" style="color:darkblue; font-weight:normal !important">(Enter Federal Tax ID) or (SSN) 9 numbers, do not enter dashes or spaces.</label>
        <br />
        <br />
        <span style="margin-right:9%"></span>
        <label for="label6" style="color:darkblue; font-weight:normal !important">If you don't know Email Address, then please call Helpdesk at 1-800-000-0000</label>
        <br />
        <br />
        <span style="margin-left:20%"></span>
        <button type="submit" style="width: 20%; color: white; background-color: deepskyblue; border-color:black" value="SubmitRequest" name="Submitbtn"><b>Submit</b></button>
        <span style="margin-left:20%"></span>
        <button type="reset" style="width:20%; background-color:deepskyblue; color:white; border-color:black"><b>Clear</b></button>
        <br />
        <br />
        <br />
        <span style="margin-left:10%"></span>
        <label for="label6" style="color:red; font-weight:normal !important; visibility: hidden">Email Address or the Login ID does not match. Please verify and enter the details again.</label>
    </div>
}

我被告知我的視圖必須至少提供模型中所有必需字段的字段。 有人可以幫我弄這個嗎。

在您的視圖中,您應該添加 (name="") 以選擇:

        <select id="taxid" name="TaxID" style="width:10%">
        <option value="fein">FEIN</option>
        <option value="ssn">SSN</option>
        </select>

同樣在模型中 TaxId 是 (int) 但在選擇框值是 (string),您將模型中的 int 更改為字符串,另外最好在選擇上添加 (Required),因為在模型中它是必需的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM