-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 };
tereško
  • 58,060
  • 25
  • 98
  • 150
user3189899
  • 167
  • 2
  • 10
  • That gives you a compiler error, which will give you the answer if you search the web for it: use `id.Value`. – CodeCaster Mar 01 '14 at 14:39
  • 1
    What should be assigned to `IncidentID` when `id` is not passed? – Sergey Berezovskiy Mar 01 '14 at 14:39
  • You would have to change your `IncidentID` property on `class Job` `int?` as well. – StuartLC Mar 01 '14 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. – Mike Cheel Mar 01 '14 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. – user3189899 Mar 01 '14 at 14:54
  • _"IncidentID will be defined by user input"_ - then why make it nullable? – CodeCaster Mar 01 '14 at 14:57
  • Because I want to methods to create a job, one where the incident ID is received via call of create method from an incident record and also the option to create an job and define the IncidentID in the form. Should I just have a method for each option – user3189899 Mar 01 '14 at 15:07

3 Answers3

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 };
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • 1
    How do you know `0` should be assigned in case id was not passed? – Sergey Berezovskiy Mar 01 '14 at 14:46
  • I am going to add there is an overload for GetValueOrDefault where the default value can be specified. http://msdn.microsoft.com/en-us/library/3d6d4f1d(v=vs.110).aspx – Mike Cheel Mar 01 '14 at 14:47
  • I have updated my answer guy's – Ramesh Rajendran Mar 01 '14 at 14:47
  • 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 – Sergey Berezovskiy Mar 01 '14 at 14:49
  • 2
    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](http://stackoverflow.com/questions/5995317/how-to-convert-nullable-int-to-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. – CodeCaster Mar 01 '14 at 14:50
  • yes i already did, and updated some another way ... – Ramesh Rajendran Mar 01 '14 at 14:52
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);
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
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
}
Shyju
  • 214,206
  • 104
  • 411
  • 497