Aspnet Core MVC Validation messages always visible
up vote
0
down vote
favorite
I'm implementing form validation, my model looks like this:
public class Profile
{
[Required]
public string Firstname { get; set; }
}
my view looks like this:
<form asp-action="SaveProfile" asp-controller="User" asp- profile="@Model" asp-file="file" enctype="multipart/form-data" method="POST">
<label asp-for="Firstname">Firstname</label>
<input class="form-control" id="firstname" asp-for="Firstname" placeholder="Firstname">
<span asp-validation-for="Firstname"></span>
<input class="form-control" type="submit" value="Save">
</form>
My controller:
public async Task<IActionResult> Index(Profile profile)
{
return View(await _userRepository.ReadProfile(User.Identity.Name));
}
and ReadProfile()
:
public async Task<Profile> ReadProfile(string user)
{
var profile = await _context.User
.Where(u => u.Email == user)
.Include(s => s.Schedule)
.Include(s => s.Skills)
.FirstOrDefaultAsync() ?? await CreateProfile(user);
var schedule = profile.Schedule.OrderBy(sd => sd.StartDate).ToList();
profile.Schedule = schedule;
return profile;
}
When I load the view I see Firstname is required
even if the field contains data on submission
any ideas?
asp.net-mvc validation razor asp.net-core
|
show 1 more comment
up vote
0
down vote
favorite
I'm implementing form validation, my model looks like this:
public class Profile
{
[Required]
public string Firstname { get; set; }
}
my view looks like this:
<form asp-action="SaveProfile" asp-controller="User" asp- profile="@Model" asp-file="file" enctype="multipart/form-data" method="POST">
<label asp-for="Firstname">Firstname</label>
<input class="form-control" id="firstname" asp-for="Firstname" placeholder="Firstname">
<span asp-validation-for="Firstname"></span>
<input class="form-control" type="submit" value="Save">
</form>
My controller:
public async Task<IActionResult> Index(Profile profile)
{
return View(await _userRepository.ReadProfile(User.Identity.Name));
}
and ReadProfile()
:
public async Task<Profile> ReadProfile(string user)
{
var profile = await _context.User
.Where(u => u.Email == user)
.Include(s => s.Schedule)
.Include(s => s.Skills)
.FirstOrDefaultAsync() ?? await CreateProfile(user);
var schedule = profile.Schedule.OrderBy(sd => sd.StartDate).ToList();
profile.Schedule = schedule;
return profile;
}
When I load the view I see Firstname is required
even if the field contains data on submission
any ideas?
asp.net-mvc validation razor asp.net-core
Can you reproduce this in a separate project - just out of the box project with Model and View from your post?
– Nemanja Todorovic
Nov 22 at 15:52
Are you getting the message when you load your page (GET action) or when you submit your form
– Shyju
Nov 22 at 17:58
@Shyju both on load and submit
– Mark Pendlebury
Nov 22 at 20:13
That could only happen if you have added aModelState
error. Most likely because your GET method includes a parameter for your model (show the relevant code)
– user3559349
Nov 22 at 20:38
1
Remove theProfile profile
parameter from yourIndex
method (theDefaultModelBinder
initializes that model, sets its properties from the request - in your case you did not call it using../Index?Firstname=someValue
- and because you have a required property aModelState
error is added)
– user3559349
Nov 22 at 21:08
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm implementing form validation, my model looks like this:
public class Profile
{
[Required]
public string Firstname { get; set; }
}
my view looks like this:
<form asp-action="SaveProfile" asp-controller="User" asp- profile="@Model" asp-file="file" enctype="multipart/form-data" method="POST">
<label asp-for="Firstname">Firstname</label>
<input class="form-control" id="firstname" asp-for="Firstname" placeholder="Firstname">
<span asp-validation-for="Firstname"></span>
<input class="form-control" type="submit" value="Save">
</form>
My controller:
public async Task<IActionResult> Index(Profile profile)
{
return View(await _userRepository.ReadProfile(User.Identity.Name));
}
and ReadProfile()
:
public async Task<Profile> ReadProfile(string user)
{
var profile = await _context.User
.Where(u => u.Email == user)
.Include(s => s.Schedule)
.Include(s => s.Skills)
.FirstOrDefaultAsync() ?? await CreateProfile(user);
var schedule = profile.Schedule.OrderBy(sd => sd.StartDate).ToList();
profile.Schedule = schedule;
return profile;
}
When I load the view I see Firstname is required
even if the field contains data on submission
any ideas?
asp.net-mvc validation razor asp.net-core
I'm implementing form validation, my model looks like this:
public class Profile
{
[Required]
public string Firstname { get; set; }
}
my view looks like this:
<form asp-action="SaveProfile" asp-controller="User" asp- profile="@Model" asp-file="file" enctype="multipart/form-data" method="POST">
<label asp-for="Firstname">Firstname</label>
<input class="form-control" id="firstname" asp-for="Firstname" placeholder="Firstname">
<span asp-validation-for="Firstname"></span>
<input class="form-control" type="submit" value="Save">
</form>
My controller:
public async Task<IActionResult> Index(Profile profile)
{
return View(await _userRepository.ReadProfile(User.Identity.Name));
}
and ReadProfile()
:
public async Task<Profile> ReadProfile(string user)
{
var profile = await _context.User
.Where(u => u.Email == user)
.Include(s => s.Schedule)
.Include(s => s.Skills)
.FirstOrDefaultAsync() ?? await CreateProfile(user);
var schedule = profile.Schedule.OrderBy(sd => sd.StartDate).ToList();
profile.Schedule = schedule;
return profile;
}
When I load the view I see Firstname is required
even if the field contains data on submission
any ideas?
asp.net-mvc validation razor asp.net-core
asp.net-mvc validation razor asp.net-core
edited Nov 22 at 21:04
asked Nov 22 at 14:58
Mark Pendlebury
11
11
Can you reproduce this in a separate project - just out of the box project with Model and View from your post?
– Nemanja Todorovic
Nov 22 at 15:52
Are you getting the message when you load your page (GET action) or when you submit your form
– Shyju
Nov 22 at 17:58
@Shyju both on load and submit
– Mark Pendlebury
Nov 22 at 20:13
That could only happen if you have added aModelState
error. Most likely because your GET method includes a parameter for your model (show the relevant code)
– user3559349
Nov 22 at 20:38
1
Remove theProfile profile
parameter from yourIndex
method (theDefaultModelBinder
initializes that model, sets its properties from the request - in your case you did not call it using../Index?Firstname=someValue
- and because you have a required property aModelState
error is added)
– user3559349
Nov 22 at 21:08
|
show 1 more comment
Can you reproduce this in a separate project - just out of the box project with Model and View from your post?
– Nemanja Todorovic
Nov 22 at 15:52
Are you getting the message when you load your page (GET action) or when you submit your form
– Shyju
Nov 22 at 17:58
@Shyju both on load and submit
– Mark Pendlebury
Nov 22 at 20:13
That could only happen if you have added aModelState
error. Most likely because your GET method includes a parameter for your model (show the relevant code)
– user3559349
Nov 22 at 20:38
1
Remove theProfile profile
parameter from yourIndex
method (theDefaultModelBinder
initializes that model, sets its properties from the request - in your case you did not call it using../Index?Firstname=someValue
- and because you have a required property aModelState
error is added)
– user3559349
Nov 22 at 21:08
Can you reproduce this in a separate project - just out of the box project with Model and View from your post?
– Nemanja Todorovic
Nov 22 at 15:52
Can you reproduce this in a separate project - just out of the box project with Model and View from your post?
– Nemanja Todorovic
Nov 22 at 15:52
Are you getting the message when you load your page (GET action) or when you submit your form
– Shyju
Nov 22 at 17:58
Are you getting the message when you load your page (GET action) or when you submit your form
– Shyju
Nov 22 at 17:58
@Shyju both on load and submit
– Mark Pendlebury
Nov 22 at 20:13
@Shyju both on load and submit
– Mark Pendlebury
Nov 22 at 20:13
That could only happen if you have added a
ModelState
error. Most likely because your GET method includes a parameter for your model (show the relevant code)– user3559349
Nov 22 at 20:38
That could only happen if you have added a
ModelState
error. Most likely because your GET method includes a parameter for your model (show the relevant code)– user3559349
Nov 22 at 20:38
1
1
Remove the
Profile profile
parameter from your Index
method (the DefaultModelBinder
initializes that model, sets its properties from the request - in your case you did not call it using ../Index?Firstname=someValue
- and because you have a required property a ModelState
error is added)– user3559349
Nov 22 at 21:08
Remove the
Profile profile
parameter from your Index
method (the DefaultModelBinder
initializes that model, sets its properties from the request - in your case you did not call it using ../Index?Firstname=someValue
- and because you have a required property a ModelState
error is added)– user3559349
Nov 22 at 21:08
|
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433622%2faspnet-core-mvc-validation-messages-always-visible%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you reproduce this in a separate project - just out of the box project with Model and View from your post?
– Nemanja Todorovic
Nov 22 at 15:52
Are you getting the message when you load your page (GET action) or when you submit your form
– Shyju
Nov 22 at 17:58
@Shyju both on load and submit
– Mark Pendlebury
Nov 22 at 20:13
That could only happen if you have added a
ModelState
error. Most likely because your GET method includes a parameter for your model (show the relevant code)– user3559349
Nov 22 at 20:38
1
Remove the
Profile profile
parameter from yourIndex
method (theDefaultModelBinder
initializes that model, sets its properties from the request - in your case you did not call it using../Index?Firstname=someValue
- and because you have a required property aModelState
error is added)– user3559349
Nov 22 at 21:08