Aspnet Core MVC Validation messages always visible












0















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?










share|improve this question

























  • 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 '18 at 15:52











  • Are you getting the message when you load your page (GET action) or when you submit your form

    – Shyju
    Nov 22 '18 at 17:58











  • @Shyju both on load and submit

    – Mark Pendlebury
    Nov 22 '18 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 '18 at 20:38








  • 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 '18 at 21:08
















0















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?










share|improve this question

























  • 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 '18 at 15:52











  • Are you getting the message when you load your page (GET action) or when you submit your form

    – Shyju
    Nov 22 '18 at 17:58











  • @Shyju both on load and submit

    – Mark Pendlebury
    Nov 22 '18 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 '18 at 20:38








  • 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 '18 at 21:08














0












0








0








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 21:04







Mark Pendlebury

















asked Nov 22 '18 at 14:58









Mark PendleburyMark Pendlebury

32




32













  • 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 '18 at 15:52











  • Are you getting the message when you load your page (GET action) or when you submit your form

    – Shyju
    Nov 22 '18 at 17:58











  • @Shyju both on load and submit

    – Mark Pendlebury
    Nov 22 '18 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 '18 at 20:38








  • 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 '18 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 '18 at 15:52











  • Are you getting the message when you load your page (GET action) or when you submit your form

    – Shyju
    Nov 22 '18 at 17:58











  • @Shyju both on load and submit

    – Mark Pendlebury
    Nov 22 '18 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 '18 at 20:38








  • 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 '18 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 '18 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 '18 at 15:52













Are you getting the message when you load your page (GET action) or when you submit your form

– Shyju
Nov 22 '18 at 17:58





Are you getting the message when you load your page (GET action) or when you submit your form

– Shyju
Nov 22 '18 at 17:58













@Shyju both on load and submit

– Mark Pendlebury
Nov 22 '18 at 20:13





@Shyju both on load and submit

– Mark Pendlebury
Nov 22 '18 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 '18 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 '18 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 '18 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 '18 at 21:08












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python