System.InvalidOperationException: The Nullable object must have a value. ASPP.NET MVC

I'm tired of fixing this error, but nothing works.

Here is the error:

System.InvalidOperationException: The Nullable object must have a value.
in System.ThrowHelper.ThrowInvalidOperationException (ExceptionResource resource) in System.Nullable 1.get_Value() at Finance.Calculator.Presentation.Admin.Areas.HDGS.Controllers.TransactionController.Edit(Int64 TransactionId, String returnURL) in d:\BuildAgent2\work\83d988abf03ace44\Code\Presentation\Finance.Calculator.Presentation.Admin\Areas\HDGS\Controllers\TransactionController.cs:line 44 at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) in System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod (ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary 2 parameters) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func1 continued) in System.Web.Mvc.Control. > C__DisplayClass15 <. > C__DisplayClass17.b__14 () in System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters (ControllerContext controlContext, IList 1 filters, ActionDescriptor actionDescriptor, IDictionary2) with System.Web.Mvc.ControllerActionInvoker.InvokeAction (ControllerContext controllerContext, String actionName)

Here is my code:

public ViewResult Edit(string TId, string returnURL)
    {
        long tid = !string.IsNullOrEmpty(TId) ? Convert.ToInt64(TId) : Convert.ToInt64(Request.QueryString["TId"].ToString());
        var TransactionItem = db.HDGSAccTransaction.SingleOrDefault(t => t.TransactionID.Equals(tid));
        TransactionVM oTrns = null;
        if (!string.IsNullOrEmpty(returnURL))
        {
            TempData["ReturnUrl"] = returnURL;
        }
        if (TransactionItem != null)
        {
            oTrns = new TransactionVM
            {
                TRef = TransactionItem.dimTransaction.TRef,
                Yr = TransactionItem.Yr,
                IcV = (decimal)TransactionItem.IcV,
                OFv = (decimal)TransactionItem.OFv,
                OCBv = (decimal)TransactionItem.OCBv,
                OOBv = (decimal)TransactionItem.OOBv,
                OInv = (decimal)TransactionItem.OInv
            };
        }

        return View(oTrns);
    }

Please note: this code works fine in the Visual Studio development environment, but not on the servers during deployment.

+4
3

, , , , , nullables null.

a Nullable<T> - null, , . " Nullable ". ()

. null, decimal.MinValue:

// ...
IcV = TransactionItem.IcV.HasValue ? TransactionItem.IcV.Value : decimal.MinValue,
// ...

, :

public static T SafeCast<T>(this Nullable<T> t, T defaultValue = default(T)) where T : struct
{
    return t.HasValue ? t.Value : defaultValue;
}

:

IcV = TransactionItem.IcV.SafeCast(decimal.MinValue),
+7

, , , . nullable .

, nullable , .

0

, Nullable SQL Server Money : , (c.AmountAgreedOn), , td, :

   @{
    string value = "";
    decimal? decimalValue = c.AmountAgreedOn;
    if (decimalValue.HasValue)
    {
         value = ((decimal)(decimalValue)).ToString("0.00");
        <td align="center">@value</td>
    }
    else
    {
               <td align="center">@value</td>
    }
}

, "" , .

.,, model.cs "AmountAgreedOn":

[DisplayFormat(ApplyFormatInEditMode = false, DataFormatString = "{0:c}")]
public Nullable<decimal> AmountAgreedOn { get; set; }

( , ), ApplyFormatInEditMode "true".

Not very, but I could not understand another way. I just don’t understand the mad scientist.

0
source

Source: https://habr.com/ru/post/1546954/


All Articles