1

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.

7
  • There is no point at all, because your method expects a value for resumeId (and if it were null, then nothing would be deleted). Change the parameter to int resumeId. Commented Sep 7, 2017 at 4:39
  • @StephenMuecke: Can you kindly tell me a real world scenario, where I should be using it and where I should avoid it? Commented Sep 7, 2017 at 4:40
  • resumeId is 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. Commented Sep 7, 2017 at 4:41
  • You might have a method that required one parameter, and a 2nd optional parameter for example, so the 2nd one would be nullable Commented Sep 7, 2017 at 4:42
  • If I do something like 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.. Commented Sep 7, 2017 at 4:46

2 Answers 2

1

There is no value in making the parameter nullable in your case. Its a POST method for deleting a record based on its Id so your expecting an Id to be provided, and a null value would make no sense.

What however you should be doing is checking that the current user has the permission to delete a record with that Id, and checking that the record returned by your query is not null before calling the context to delete the record so that you protected against malicious users (or the case where another user may have deleted the same record in the meantime).

[HttpPost] [ValidateAntiForgeryToken] public ActionResult Delete(int resumeId) { ... // check if the user has permission to delete the Resume with this id var r = _context.Resumes.Where(c => c.ResumeId == resumeId).SingleOrDefault(); if (r != null) { _context.Resumes.Remove(r); _context.SaveChanges(); } return RedirectToAction("ResumeCenter"); } 

Note that the example you linked to is a GET method, and it may be appropriate to have nullable parameters, for example a method that filters a set of records based on a CategoryId where a value of null means return all records, as opposed to returning only records that have the matching CategoryId

Sign up to request clarification or add additional context in comments.

Comments

-1

Ok, look here. You have route like this: .../Products/Update?id=0 So if param id is null or id==0 you create product else you update product.

[HttpPost] [ValidateAntiForgeryToken] public ActionResult Update(int? id=0) { if(id==0) create something else update return RedirectToAction("ResumeCenter"); } 

int? id=0 mean that if action do not take any argument id becomes equal 0. It`s mean that this param is optional.

3 Comments

Do we need to set it as 0? It should be done by default right?
Theres no point of using a nullable if you set it to 0 by default
if you use post no data to ../products/update action will take id as null value without exception.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.