需要将可空双精度合并为非可空方法

时间:2014-01-04 16:48:33

标签: c# asp.net-mvc

我创建了2 ActionResult,两者都接受相同的参数,除了一个需要可以为空,一个不可以。当我将表单提交传递给ActionResult时,问题就出现了,c#无法区分使用哪个ActionResult。有人能告诉我如何将这两者合并在一起所以我可以让一个ActionResult接受一个可以为空的双重而不会导致我“Cannot implicitly convert between double? and double

我的不可为空的ActionResult如下:

public ActionResult Convert(double p1, string p2)
{
    Exercise07IndexViewModel model = new Exercise07IndexViewModel { Conv = p1 };

    if( p2 == "C2F" )
    {
        var math = ((9 * p1) / 5 ) + 32;
        Exercise07ConvertViewModel c2f
        = new Exercise07ConvertViewModel { OriginalValue = p1, OriginalUnit = "°C", ConvertedValue = math, ConvertedUnit = "°F", Convert = p2 };
        return View("Convert", c2f);
    }
    else if (p2 == "F2C")
    {
        var math = ((p1 - 32) * 5) / 9;
        Exercise07ConvertViewModel f2c
        = new Exercise07ConvertViewModel { OriginalValue = p1, OriginalUnit = "°F", ConvertedValue = math, ConvertedUnit = "°C", Convert = p2 };

        return View(f2c);

    }
    else if (p2 == "oz2g")
    {
        var math = 28.35 * p1;
        Exercise07ConvertViewModel oz2g
        = new Exercise07ConvertViewModel { OriginalValue = p1, OriginalUnit = "oz", ConvertedValue = math, ConvertedUnit = "g", Convert = p2 };
        return View("Convert", oz2g);
    }
    else if (p2 == "g2oz")
    {
        var math = 0.035 * p1;
        Exercise07ConvertViewModel g2oz
       = new Exercise07ConvertViewModel { OriginalValue = p1, OriginalUnit = "g", ConvertedValue = math, ConvertedUnit = "oz", Convert = p2 };
        return View("Convert", g2oz);
    }
    else if (p2 == null || p2 == String.Empty || p2 == "ScaleNotSupported")
    {
        return RedirectToAction("Index", "Home");
    }
    else
    {
        return View("Convert", model);
    }
}

我可以理解的ActionResult,我需要包含在上面的内容中:

public ActionResult Convert(double? p1, String p2)
{
    if(p1 == null)
    {
        return RedirectToRoute(new
        {
            controller = "Index",
            action = "Index"
        });
    }
    return View();
 }

2 个答案:

答案 0 :(得分:1)

如果我正确理解您的问题,很简单:从doubledouble?进行隐式转换,因此您只能使用nullable-double方法离开:

public ActionResult Convert(double? np1, string p2)
{
    if (np1 == null)
    {
        return RedirectToRoute(new
        {
            controller = "Index",
            action = "Index"
        });
    }

    var p1 = np1.GetValueOrDefault();
    Exercise07IndexViewModel model = new Exercise07IndexViewModel { Conv = p1 };

    if( p2 == "C2F" )
    {
        var math = ((9 * p1) / 5 ) + 32;
        Exercise07ConvertViewModel c2f = new Exercise07ConvertViewModel { OriginalValue = p1, OriginalUnit = "°C", ConvertedValue = math, ConvertedUnit = "°F", Convert = p2 };
        return View("Convert", c2f);
    }
    else if (p2 == "F2C")
    {
        var math = ((p1 - 32) * 5) / 9;
        Exercise07ConvertViewModel f2c = new Exercise07ConvertViewModel { OriginalValue = p1, OriginalUnit = "°F", ConvertedValue = math, ConvertedUnit = "°C", Convert = p2 };
        return View(f2c);
    }
    else if (p2 == "oz2g")
    {
        var math = 28.35 * p1;
        Exercise07ConvertViewModel oz2g = new Exercise07ConvertViewModel { OriginalValue = p1, OriginalUnit = "oz", ConvertedValue = math, ConvertedUnit = "g", Convert = p2 };
        return View("Convert", oz2g);
    }
    else if (p2 == "g2oz")
    {
        var math = 0.035 * p1;
        Exercise07ConvertViewModel g2oz = new Exercise07ConvertViewModel { OriginalValue = p1, OriginalUnit = "g", ConvertedValue = math, ConvertedUnit = "oz", Convert = p2 };
        return View("Convert", g2oz);
    }
    else if (String.IsNullOrEmpty(p2) || p2 == "ScaleNotSupported")
    {
        return RedirectToAction("Index", "Home");
    }
    else
    {
        return View("Convert", model);
    }
}

答案 1 :(得分:0)

所有可空类型都具有相同的属性,可用于将它们转换为不可空的等价物,例如:

public ActionResult Convert(double? p1, String p2)
{
  if (p1.HasValue)
  {
    var d = p1.Value;
    // Do the equivalent of Convert(d, p2)
  }
  else
  {
    // Handle the null p1.
  }
}