ASP.NET MVC3 HtmlPassword如何使其在post方法后不重置

时间:2012-11-01 14:42:11

标签: asp.net-mvc asp.net-mvc-3 security passwords


基本上我有一个HtmlPasswordFor(m => EmployeeID),我有一个名为“Go”的按钮。一旦我点击Go文本框,我实际上不希望密码消失或者要清除字段密码。
我该怎么办呢?

之前 enter image description here


enter image description here 我不想让我的员工重置。我想把密码保存在****中,但我下次打电话时需要密码。

控制器

    [HttpGet]
    public ActionResult MainForm()
    {
        var model = new VTViewModel();
        return View(model);
    }

    [HttpPost]
    public ActionResult MainForm(VTViewModel model, string buttontype)
    {
        if (ModelState.IsValid)
        {
            string EmployeeID = (Convert.ToInt64(model.EmployeeBinaryID, 2)).ToString();
            if (buttontype == "Go")
            {
                // GET Fields depending on the serial number.

                model.ListFields = logic_model.getFormInfo(model.SerialNumber, EmployeeID);
                if (model.ListFields[0].return_num == "0")
                {
                    model.Go = true;
                    // Set Process
                    // Set Header Token
                    // Set Header Title

                    ViewData["HEADER"] = model.ListFields[0].HeaderName + " | " + model.ListFields[0].TubeType + " | " + model.ListFields[0].ProductLine;
                }
                else
                {
                    model.DisplayMessage = helper.checkErrors(model.ListFields[0].return_num);
                }
            }
            else if (buttontype == "Submit")
            {
                model.HeaderToken = model.ListFields[0].HeaderToken; 
                string header = model.HeaderToken;
                string check = "";                                                        
                string return_num = model.ListFields[0].return_num; 
                // If the submission worked

                //SUBMIT HERE INTO DB then Clear
                List<string> values_to_submit = new List<string>(); // Creates a list to store the values to submit
                for (int i = 0; i < model.ListFields.Count; i++)
                {
                    // Fills in the hidden values.
                    if (model.ListFields[i].isHidden)
                    {
                        if (model.ListFields[i].Token == "SNT" || model.ListFields[i].Token == "SNC")
                        {
                            model.ListFields[i].Value = model.SerialNumber;
                        }
                        else if (model.ListFields[i].Token == "USR")
                        {
                            model.ListFields[i].Value = EmployeeID;
                        }
                        else if (model.ListFields[i].Token == "TMS")
                        {
                            model.ListFields[i].Value = "0";
                        }
                    }

                    // If it is a check box do the right conversion.
                    if (model.ListFields[i].DataType == "DATA-CKBOX")
                    {
                        //string convertedValue = helper.trueFalseStringtToIntBool(model.ListFields[i].Value);
                        string convertedValue = helper.boolToInt(model.ListFields[i].BoolValue).ToString();
                        model.ListFields[i].Value = convertedValue;
                    }

                    values_to_submit.Add(model.ListFields[i].Token + model.ListFields[i].Value);

                }

                check = logic_model.helperSubmit(values_to_submit, header, 1);



                if (check == "Submitted")
                {
                    ModelState.Clear();
                    VTViewModel clear_model = new VTViewModel(); // Creates an empty model
                    clear_model.DisplayMessage = "Submitted\n" + model.SerialNumber + "\n" + DateTime.Now;
                    return View(clear_model);
                }
                else
                {
                    model.DisplayMessage = check; // Sets the display message to the error.
                }

            }
            else if (buttontype == "Clear")
            {
                // Clears the screen and model
                ModelState.Clear();
                VTViewModel clear_model = new VTViewModel(); // Creates an empty model
                return View(clear_model);
            }
        }
        return View(model);

    }

视图

    div>
         <fieldset>
         <table id="main_table">
                <tr>
                    <td>@Html.LabelFor(m => m.SerialNumber)</td>
                    <td>@Html.LabelFor(m => m.EmployeeBinaryID)</td>
                </tr>
                <tr>
                    <td>@Html.TextBoxFor(m => m.SerialNumber)</td>
                    <td>@Html.PasswordFor(m => m.EmployeeBinaryID, new { autocomplete = "off" }) </td>
                </tr>

                <tr><td><input type="submit" value="Go" name="buttontype" class="submit_button" id="btn"/><br /><br /></td></tr>
         </table>
         </fieldset>
         <br /> 



感谢

1 个答案:

答案 0 :(得分:2)

Html.PasswordFor永远不会渲染该值,可能是出于安全原因。您可以解决此问题,但必须使用Html.EditorFor,用EmployeeID修饰[DataType(DataType.Password)]属性,并在〜/ Views / Shared / EditorTemplates / Password.cshtml中添加以下编辑器模板

@Html.Password("", ViewData.TemplateInfo.FormattedModelValue, new { @class = "text-box single-line password" })

您也可以使用[UIHint]并仅为该特定属性创建模板。