At many places I see controller action with Nullable Int as parameter. I have known from SO research that I should put Model Propetry as Nullable and Required. It helps to protect from Underposting attack and also, it helps us to avoid seeing "Default values" of property in UI Form, such as for datetime property.
[Required] public DateTime? dateTime {get;set;} With above set up I will now not see the defaulted date. So far so good. But what is the significance of using "?" in ControllerAction? And when shall I use it.
Currently, I have a Delete functionality and I have written below code
[HttpPost] [ValidateAntiForgeryToken] public ActionResult Delete(int? resumeId) { var r = _context .Resumes .Where(c => c.ResumeId == resumeId).SingleOrDefault(); _context.Resumes.Remove(r); _context.SaveChanges(); return RedirectToAction("ResumeCenter"); } Can someone guide me on when do I need to use "?" and it's significance? I read this Nullable Int link, but I could not understand. Kindly give me an example for both usage.
resumeId(and if it werenull, then nothing would be deleted). Change the parameter toint resumeId.resumeIdis an action parameter serves to query the DB. If this parameter is nullable, you need check for null values passed on it. Using non-nullable int may be wise since delete operation requires an ID to find the respective record which should be deleted.public void SomeMethod(int a, int b = 0), Here we are forcing a default value. But with "?" there will be no value at all. I see..