ASP.NET MVC3 HtmlPasswordFor how to make it not re

2019-08-12 05:24发布

问题:


Essentially I have a HtmlPasswordFor(m => EmployeeID), I have a button called "Go". Once I hit the Go textbox, I actually do not want the password to disappear on the or have the field password to be cleared.
How would I do this?


BEFORE


AFTER I do not want the my employeeid to reset. I want to keep the passwords in ****'s, but I'll need the password on my next post call.

Controller

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

    }

View

    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 /> 



Thanks

回答1:

Html.PasswordFor will never render the value, probably for security reasons. You can work around this, but you have to use Html.EditorFor instead, decorate the EmployeeID property with [DataType(DataType.Password)] and add the following editor template at ~/Views/Shared/EditorTemplates/Password.cshtml

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

You could also use [UIHint] and create a template for that particular property only.