Reputation: 141
I have a form in a view with various inputs, although one of these inputs must not be null
(TeacherName)
. If a user leaves this field blank, the model state will not be valid so they will be passed back to the HttpGet BookingForm
action (some object information is passed back with the user; RollNumber
, Date
and a Validation Message
).
When the user is passed back to the Form I need to show the ValidationMsg
in the view at the top of the form. Although I can not get the message to show up in the view. However, the ValidationMsg
is being passed in the URL as I can see this in the URL: 0&ValidationMsg=Please%20complete%20all%20fields
Can anyone tell me why the ValidationMsg
is not showing up in the View when the user gets returned RedirectToAction
to BookingForm
?
My controllers:
[HttpGet]
public ActionResult BookingForm(School model)
{
School school = db.Schools.First(m => m.RollNumber ==
model.RollNumber);
return View(school);
}
[HttpPost]
[ActionName("BookingForm")]
public ActionResult BookingFormPost(Booking model)
{
if (ModelState.IsValid)
{
db.Bookings.Add(model);
db.SaveChanges();
return RedirectToAction("Index","Home");
}
var newModel = new School();
newModel.RollNumber = model.RollNumber;
newModel.Date = model.Date;
string ValidateMsg = "Please complete all fields";
newModel.ValidationMsg = ValidateMsg;
return RedirectToAction("BookingForm", newModel);
}
My model:
public int Id { get; set; }
public string RollNumber { get; set; }
public string OfficialSchoolName { get; set; }
[Required(ErrorMessage = "A teacher name is required")]
public string TeacherName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
View:
@model CampBookingSys.Models.School
@{
ViewBag.Title = "School Booking Form";
}
<h2>School Booking Form</h2>
<form asp-controller="School" asp-action="BookingForm" method="post">
<p>@Model.ValidationMsg</p>
<br />
<p>Please complete the following information</p>
<div>
<label for="RollNumber">School Roll Number:</label>
<br />
<input asp-for="Booking.Rollnumber" id="txtRoll" name="RollNumber"
value="@Model.RollNumber" />
</div>
<br />
<div>
<label for="Date">Choose Camp Date:</label>
<br />
<input asp-for="Booking.Date" id="txtDate" name="Date"
value="@Model.Date.Value.ToShortDateString()" />
</div>
<br />
<div>
<label for="OfficialSchoolName">School Name:</label>
<br />
<input asp-for="Booking.OfficialSchoolName" id="txtOfficialSchoolName"
name="OfficialSchoolName" value="@Model.OfficialSchoolName" />
</div>
<br />
<div>
<label for="TeacherName">Teacher Name:</label>
<br />
<input asp-for="Booking.TeacherName" id="txtTeacherName"
name="TeacherName" />
</div>
<br />
<div>
<label for="Email">Teacher Email:</label>
<br />
<input asp-for="Booking.Email" id="txtEmail" name="Email" />
</div>
<br />
<div>
<label for="PhoneNumber">Teacher Contact Number:</label>
<br />
<input asp-for="Booking.PhoneNumber" id="txtPhoneNumber"
name="PhoneNumber" value="@Model.PhoneNumber" />
</div>
<br />
<div>
<label for="Surveys">Would you like to take part in research surveys?
</label>
<br />
<input asp-for="Booking.Surveys" id="Surveys" name="Surveys"
type="checkbox" onclick="$(this).val(this.checked ? true : false)" />
</div>
<br />
<input type="submit" value="Submit" class="submit" />
</form>
Cheers
Upvotes: 0
Views: 58
Reputation: 9764
As you are populating the school model in the Post
action. You don't have to use RedirectToAction
instead use return View
itself. It should work.
In your HttpGet
action you are repopulating your school model, that is why RedirectToAction
not working.
[HttpPost]
[ActionName("BookingForm")]
public ActionResult BookingFormPost(Booking model)
{
if (ModelState.IsValid)
{
db.Bookings.Add(model);
db.SaveChanges();
return RedirectToAction("Index","Home");
}
var newModel = new School();
newModel = db.Schools.First(m => m.RollNumber == model.RollNumber);
newModel.Date = model.Date;
string ValidateMsg = "Please complete all fields";
newModel.ValidationMsg = ValidateMsg;
return View("BookingForm", newModel);
}
Upvotes: 1