Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net MVC repopulate form without using html helpers

If i submit the form without filling some of the mandatory fields, when the page reloads again with errors, the fields that were filled out retain their values, but only if I use html helpers like so:

@using (Html.BeginForm())
{
    @Html.ValidationSummary()
    <p>Your name: @Html.TextBoxFor(x => x.Name) </p>
    <p>Your email: @Html.TextBoxFor(x => x.Email)</p>
    <p>Your phone: @Html.TextBoxFor(x => x.Phone)</p>
    <p>
        @Html.DropDownListFor(x => x.WillAttend, new[] {

        <input          new SelectListItem() {
                            Text = "Yes, I'll be there",
                            Value = bool.TrueString},
                        new SelectListItem() {
                            Text = "No, I can't come",
                            Value = bool.FalseString}
                        }, "Choose an option")
    </p> type="submit" value="Submit RSVP" />
}

But I want to use regular html and not helpers like so (and then the values aren't remembered):

<form action="/Home/RsvpForm" method="post">
    @Html.ValidationSummary()
    <p>
        Your name:
        <input type="text" name="Name" value="" />
    </p>
    <p>
        Your email:
        <input type="text" name="Email" value="" />
    </p>
    <p>
        Your phone:
        <input type="text" name="Phone" value="" />
    </p>
    <p>
        Will you attend?
        <select name="WillAttend">
            <option value="">Choose an option</option>
            <option value="true">Yes</option>
            <option value="false">No</option>
        </select>
    </p>
    <button>Submit</button>
</form>

So how can I make the form remember the correctly entered values without using html helpers, is it possible?

EDIT: the model and and the whole view

GuestResponse model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace PartyInvites.Models
{
    public class GuestResponse
    {
        [Required(ErrorMessage="Enter name")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Enter email")]
        public string Email { get; set; }

        [Required(ErrorMessage = "Enter phone")]
        public string Phone { get; set; }

        [Required(ErrorMessage = "Specify")]
        public bool? WillAttend { get; set; }



    }
}

The whole view:

@model PartyInvites.Models.GuestResponse

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>RsvpForm</title>
</head>
<body>
    <form action="/Home/RsvpForm" method="post">
        @Html.ValidationSummary()
        <p>
            Your name:
            <input type="text" name="Name" value="" />
        </p>
        <p>
            Your email:
            <input type="text" name="Email" value="" />
        </p>
        <p>
            Your phone:
            <input type="text" name="Phone" value="" />
        </p>
        <p>
            Will you attend?
            <select name="WillAttend">
                <option value="">Choose an option</option>
                <option value="true">Yes</option>
                <option value="false">No</option>
            </select>
        </p>
        <button>Submit</button>
    </form>
</body>
</html>
like image 325
Ollicca Mindstorm Avatar asked Nov 26 '25 17:11

Ollicca Mindstorm


2 Answers

You can use regular html, just set the value of input text, to receive your model.property like:

<input type="text" name="Phone" value="@Model.Phone" />
like image 127
Maturano Avatar answered Nov 28 '25 17:11

Maturano


Something like:

@model your_model 

 <form action="/Home/RsvpForm" method="post">
@Html.ValidationSummary()
<p>
    Your name:
    <input type="text" name="Name" value="@Model.Name" />
</p>
<p>
    Your email:
    <input type="text" name="Email" value="@Model.Email" />
</p>
<p>
    Your phone:
    <input type="text" name="Phone" value="@Model.Phone" />
</p>
<p>
    Will you attend?
    <select name="WillAttend">
        <option value="">Choose an option</option>
        <option value="true">Yes</option>
        <option value="false">No</option>
    </select>
</p>
<button>Submit</button>

like image 42
Emmanuel N Avatar answered Nov 28 '25 17:11

Emmanuel N



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!