razor-2asp.net-webpageswebmatrix-2

Keep input data after posting


I have an issue with a long form I'm doing. If a user fills out the form, and validation is invalid, it erases everything in the form, and doesn't keep the data in the valid forms. How would I make it so it doesn't erase the data upon pressing submit?

@{
    Layout = "~/Pages/_SiteLayout.cshtml";

    var db = Database.Open("mtarplspd");

    Validation.RequireField("TypeOfIncident", "Type of incident is required.");
    Validation.RequireField("FirstName", "Firstname is required.");
    Validation.RequireField("LastName", "Lastname is required.");
    Validation.RequireField("Rank", "Rank is required.");
    Validation.RequireField("Badge", "Badge is required.");
    Validation.RequireField("SuspectFirstName", "Suspect firstname is required.");
    Validation.RequireField("SuspectLastName", "Suspect lastname is required.");
    Validation.RequireField("SuspectCharges", "Suspect charges is required.");
    Validation.RequireField("Description", "Description of the incident is required.");

    var typeOfIncident = "";
    var firstName = "";
    var lastName = "";
    var rank = "";
    var badge = "";
    var suspectFirstName = "";
    var suspectLastName = "";
    var suspectAddress = "";
    var suspectCharges = "";
    var suspectVehicleModel = "";
    var suspectVehiclePlates = "";
    var suspectVehicleColor = "";
    var officersInvolved = "";
    var description = "";
    var departmentsInvolved = "";
    var firearmDischarged = "";
    var typeOfFirearm = "";
    var roundsFired = "";
    var reason = "";
    var userId = 0;
    var dateTime = "";
    var ErrorMessage = "";

    if (IsPost) {
        typeOfIncident = Request.Form["TypeOfIncident"];
        firstName = Request.Form["FirstName"];
        lastName = Request.Form["LastName"];
        rank = Request.Form["Rank"];
        badge = Request.Form["Badge"];
        suspectFirstName = Request.Form["SuspectFirstName"];
        suspectLastName = Request.Form["SuspectLastName"];
        suspectAddress = Request.Form["SuspectAddress"];
        suspectCharges = Request.Form["SuspectCharges"];
        suspectVehicleModel = Request.Form["SuspectVehicleModel"];
        suspectVehiclePlates = Request.Form["SuspectVehiclePlates"];
        suspectVehicleColor = Request.Form["SuspectVehicleColor"];
        officersInvolved = Request.Form["OfficersInvolved"];
        description = Request.Unvalidated["Description"];
        departmentsInvolved = Request.Form["DepartmentsInvolved"];
        firearmDischarged = Request.Form["FirearmDischarged"];
        typeOfFirearm = Request.Form["TypeOfFirearm"];
        roundsFired = Request.Form["RoundsFired"];
        reason = Request.Form["Reason"];
        userId = WebSecurity.CurrentUserId;
        dateTime = DateTime.Now.ToString("d MMM yyyy h:mm:ss");

        if (ErrorMessage == "" && Validation.IsValid()) {
            var insert = "INSERT INTO Reports (Type, FirstName, LastName, Rank, Badge, SuspectFirstName, SuspectLastName, SuspectAddress, SuspectCharges, SuspectVehicleModel, SuspectVehiclePlates, SuspectVehicleColor, OfficersInvolved, Description, DepartmentsInvolved, FirearmDischarged, TypeOfFirearm, RoundsFired, Reason, UserId, DateTime)" + "VALUES (@0, @1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13, @14, @15, @16, @17, @18, @19, @20)";
            db.Execute(insert, typeOfIncident, firstName, lastName, rank, badge, suspectFirstName, suspectLastName, suspectAddress, suspectCharges, suspectVehicleModel, suspectVehiclePlates, suspectVehicleColor, officersInvolved, description, departmentsInvolved, firearmDischarged, typeOfFirearm, roundsFired, reason, userId, dateTime);
            Response.Redirect("~/Pages/Reports/Incident/Default");
        }
    }
}

Solution

  • You need to restore the value for each form control, eg:

    <input type="text" name="FirstName" value="@Request.Form["FirstName"]" />