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.

Unbreakable
  • 7,776
  • 24
  • 90
  • 171
  • 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`. –  Sep 07 '17 at 04:39
  • @StephenMuecke: Can you kindly tell me a real world scenario, where I should be using it and where I should avoid it? – Unbreakable Sep 07 '17 at 04: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. – Tetsuya Yamamoto Sep 07 '17 at 04:41
  • You might have a method that required one parameter, and a 2nd optional parameter for example, so the 2nd one would be nullable –  Sep 07 '17 at 04: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.. – Unbreakable Sep 07 '17 at 04:46
  • Can you provide a link to an example where you saw the `int? paramterID` used? – aaronR Sep 07 '17 at 05:26
  • @aaronR: Will ping you once I come across again. There is one here though https://stackoverflow.com/questions/9906191/mvc-action-with-optional-parameters-which-is-better – Unbreakable Sep 07 '17 at 05:46

3 Answers3

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

0

I sometimes use it, in more complex controller action methods, where there is the possibility that a person did or did not selected something on the page.

In this case you want to show something else if they provided an int or not, without having multiple pages.

Another argument could be a page (think: ...controller/products?id=7) which people can share in apps or social media, where the latter part (?id=xxx) can be forgotten. In that case you will display an error page to a possible customer. What you want to do is catch that null value and redirect the user to the overview page, instead of a specific product page.

And as said by others and felt by you, for your example it does not make a lot of sense.

MrtN
  • 1
  • 3
-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.

Manfice
  • 149
  • 7