-2

Hi my create method receives an int how can I allow this to be null-able? So I can use this method without the int sometimes.

   public ActionResult Create(int id)
    {
        var model = new Job { IncidentID = id };
        ViewBag.ActionCode = new SelectList(db.ActionTypes, "ActionCode", "ActionCode");

        return View(model);
    }

Obviously I've tried

(int ? id)

but then here it is not happy as it cant convert int? to int here :

var model = new Job { IncidentID = id };
7
  • That gives you a compiler error, which will give you the answer if you search the web for it: use id.Value. Commented Mar 1, 2014 at 14:39
  • 1
    What should be assigned to IncidentID when id is not passed? Commented Mar 1, 2014 at 14:39
  • You would have to change your IncidentID property on class Job int? as well. Commented Mar 1, 2014 at 14:41
  • I'm not 100% on what you are asking BUT either IncidentID will have to support null values or you will have to convert id to a non-null value before you can make that assignment. Commented Mar 1, 2014 at 14:45
  • @Sergey Berezovskiy nothing IncidentID will be defined by user input. @ Mike Cheel its nullable for the moment but not nullable when written to the database. Commented Mar 1, 2014 at 14:54

3 Answers 3

1

try this

public ActionResult Create(int? id)
{
    var model = new Job { IncidentID = id.GetValueOrDefault(0) };
    //or var model = new Job { IncidentID = (int.parse(id) };
    ViewBag.ActionCode = new SelectList(db.ActionTypes, "ActionCode", "ActionCode");

    return View(model);
}

GetValueOrDefault(0) helps to assigns zero if id has no value or null

or try this

 var model = new Job { IncidentID = id.HasValue ? id.Value : 0 };
Sign up to request clarification or add additional context in comments.

6 Comments

How do you know 0 should be assigned in case id was not passed?
I am going to add there is an overload for GetValueOrDefault where the default value can be specified. msdn.microsoft.com/en-us/library/3d6d4f1d(v=vs.110).aspx
I have updated my answer guy's
I'll remove downvote, but that's still a guess. Maybe OP don't need new Job to be created if id was not passed
I understand you want to help OP, but OP has not provided enough information to answer his question. Your answer demonstrates one of the ways to assign a nullable int to an int, but that doesn't help OP. @Sergey's question must be answered by OP to answer this question properly, it isn't just about the assignment. Also, consider voting to close as duplicate instead of giving an answer that already exists many times.
|
0

Just check if id has value and assign IncidentID only if it has:

public ActionResult Create(int? id)
{
    var job = new Job();
    if (id.HasValue)
        job.IncidentID = id.Value;

    ViewBag.ActionCode = new SelectList(db.ActionTypes, "ActionCode", "ActionCode");
    return View(job);
}

4 Comments

I already did this sir, please see my answer
@RameshRajendran no, you don't. You are assigning 0
Yes id.HasValue is false, then i assigned 0.
@RameshRajendran should I explain difference to you?
0

You may use nullable int as your method parameter. Nullable<T> has a HasValue method which check whether a value has been assigned to the nullable variable. If it returns true, use the Value property to get the value of the variable.

public ActionResult Create(int? id)
{
  var model=new Job();
  if(id.HasValue)
  {
    model.IncidentID=id.Value;
  }
  //to do :return something
}

1 Comment

I already did this sir, please see my answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.