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>
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" />
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With