locked
System.Nullable<int> doesn't accept (int?)null RRS feed

  • Question

  • User-1865509496 posted

    My designer.cs file of all the SQL tables is generated by LINQ-TO-SQL.  The class and attribute in question is like this.

    [global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Transfers")]
    public partial class Transfer : INotifyPropertyChanging, INotifyPropertyChanged {
    ...
    private System.Nullable<int> _ListType;
    private System.Nullable<int> _ListStatus;
    ...

      [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ListType", DbType="Int")]
      public System.Nullable<int> ListType{
       
    get {
          return this._ListType;
        }
        set {
          if ((this._ListType != value)) {
            if (this._ListType1.HasLoadedOrAssignedValue){
              throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
            }
            this.OnListTypeChanging(value);
           
    this.SendPropertyChanging();
           
    this._ListType = value;
           
    this.SendPropertyChanged("ListType");
           
    this.OnListTypeChanged();
          }
        }
      }

    }

    In my controler, I have the following code. Upon executing an exception is thrown and prevent further update to the mode.

    if(transfer.ListType == 0) transfer.ListType = (int?)null;

    The ListType is bound to a DropDownList on the View page which has a blank selection with value of 0.  The property accepts null value in the controller's Create() method.

    [AcceptVerbs(HttpVerbs.Post)]
    Create(string id, Transfer newTransfer, string cancel)

    It does not accept null value in the Edit() method.

    [
    AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(string id, FormCollection collection, string cancel)

    I need to assign a null to the attribute so that the value 0 does not get save to the database.  What am I doing wrong?  Thank you for your help.

    Wednesday, July 21, 2010 5:43 PM

Answers

  • User-1865509496 posted

    Great. Thank you.  Instead of doing a major form changes, I change the ViewModel where I construct the SelectLists and changed the first item's Value property of the SelectLists to String.Empty.

    tmpTransferListTypes.Insert(0, new SelectListItem { Text = string.Empty, Value = string.Empty }); //the value was 0

    TransferListTypes = new SelectList(tmpTransferListTypes, "Value", "Text", Transfer.ListType);

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 22, 2010 12:32 PM

All replies

  • User903913376 posted

    Some of the beauty in MVC is the ability to scrap passing forms and pass your model directly to your post method. This requires properly wiring up your model to your form (naming form controls so that the names correspond to the model being updated.

    What that means is instead of passing a FormCollection you pass your bizObject

    public ActionResult Edit(string id, FormCollection collection, string cancel)

    gets replaced with public ActionResult Edit(myBizObject model)

    the Default Model Binder will wire the form values to the model you've given, provided the names are the same. What happens if the names are the same but the underlying types don't match? i.e. A dropdown list selected value is of type string when the underlying type is of type int. The answer is that it comes back null.  This works to your advantage if your dropdown has a first choice of "Select" and a value of empty string - the model will only bind the value if it is of matching type. That solves your passing zeros you don't want.

    You may want to check out the video tutorial http://www.asp.net/mvc/videos/how-do-i-work-with-model-binders-in-an-mvc-application 

    Wednesday, July 21, 2010 10:39 PM
  • User-1865509496 posted

    Great. Thank you.  Instead of doing a major form changes, I change the ViewModel where I construct the SelectLists and changed the first item's Value property of the SelectLists to String.Empty.

    tmpTransferListTypes.Insert(0, new SelectListItem { Text = string.Empty, Value = string.Empty }); //the value was 0

    TransferListTypes = new SelectList(tmpTransferListTypes, "Value", "Text", Transfer.ListType);

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 22, 2010 12:32 PM