Oct 24, 2020 01:46 AM|CopBlaster|LINK
I have a form for users to submit data, but some fields such as UserID which the model requires are added using code behind as part of the OnPostAsync event. Even though my OnPostAsync should be submitting all the required information the form is throwing
an error when I click the submit button and it posts back saying that the UserID field is required.
A sample of my code is as follows:
From Posts.cs model:
public string Userid
public async Task<IActionResult> OnPostAsync()
IdentityUser user = await _userManager.GetUserAsync(User);
Posts.Userid = await _userManager.GetUserIdAsync(user);
string targeturl = linkgenerator.postlink(Posts.Postid, Posts.Title);
There is also a problem when it requires required fields from models that only apply when specific categories are selected. When the user selects a category then a partial page is loaded via jQuery containing the inputs for that specific category. If the
user selects Shopping then they get inputs for the Shopping table, Housing for the Housing table, etc. Certain fields are marked as required the models for those tables, but they should only be required if records are being inserted into those specific tables
after the record is added to the Posts table. Then they get their data from the partial pages and Posts.Postid for Postid fields in those tables, where a relationship exists between the primary key Postid in the Posts table and a foreign key for Postid in
the category specific tables.
The problem here is that if I select a specific category like "Community" I get errors that fields required for all the other categories are missing. How do I mark something as required only if the corresponding category is selected?