' INT'不包含' HasValue'的定义

时间:2015-07-30 20:01:27

标签: c#

我无法找出代码中的错误。它说:

  

' INT'不包含' HasValue'的定义没有扩展方法' HasValue'接受类型' int'的第一个参数可以找到(你错过了使用指令或程序集引用吗?)

我检查了我的代码及其正确但我无法找出错误的来源。这是文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;
using WebApplication1.ViewModels;

namespace WebApplication1.Controllers
{
    public class EmployeeController : Controller
    {
        public string GetString()
        {
            return "Hello World is old now. It’s time for wassup bro ;)";
        }

        [NonAction]
        public string SimpleMethod()
        {
            return "Hi, I am not action method";
        }

        public ActionResult Index()
        {
            EmployeeListViewModel employeeListViewModel = new EmployeeListViewModel();

            EmployeeBusinessLayer empBal = new EmployeeBusinessLayer();
            List<Employee> employees = empBal.GetEmployees();

            List<EmployeeViewModel> empViewModels = new List<EmployeeViewModel>();

            foreach (Employee emp in employees)
            {
                EmployeeViewModel empViewModel = new EmployeeViewModel();
                empViewModel.EmployeeName = emp.FirstName + " " + emp.LastName;
                empViewModel.Salary = emp.Salary.ToString("C");

                if (emp.Salary > 15000)
                {
                    empViewModel.SalaryColor = "yellow";
                }
                else
                {
                    empViewModel.SalaryColor = "green";
                }

                empViewModels.Add(empViewModel);
            }

            employeeListViewModel.Employees = empViewModels;
         //   employeeListViewModel.UserName = "Admin";
            return View("Index", employeeListViewModel);
        }

        public ActionResult AddNew()
        {
            return View("CreateEmployee", new CreateEmployeeViewModel());
        }

        public ActionResult SaveEmployee(Employee e, string BtnSubmit)
        {
            switch (BtnSubmit)
            {
                case "Save Employee":
                    if (ModelState.IsValid)
                    {
                        EmployeeBusinessLayer empBal = new EmployeeBusinessLayer();
                        empBal.SaveEmployee(e);
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        CreateEmployeeViewModel vm = new CreateEmployeeViewModel();
                        vm.FirstName = e.FirstName;
                        vm.LastName = e.LastName;

                        if (e.Salary.HasValue)
                        {
                            vm.Salary = e.Salary.ToString();
                        }
                        else
                        {
                            vm.Salary = ModelState["Salary"].Value.AttemptedValue;
                        }

                        return View("CreateEmployee", vm); // Day 4 Change - Passing e here
                    }

                case "Cancel":
                    return RedirectToAction("Index");
            }

            return new EmptyResult();
        }

        public class MyEmployeeModelBinder : DefaultModelBinder
        {
            protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
            {
                Employee e = new Employee();
                e.FirstName = controllerContext.RequestContext.HttpContext.Request.Form["FName"];
                e.LastName = controllerContext.RequestContext.HttpContext.Request.Form["LName"];
                e.Salary = int.Parse(controllerContext.RequestContext.HttpContext.Request.Form["Salary"]);
                return e;
            }
        }
    }
}

这就是

e.Salary.HasValue

2 个答案:

答案 0 :(得分:3)

如前一篇文章所述,e.Salary是一个整数值类型,不能为null。整数类型默认为0。

您可以将if语句更改为:

Found in these nodes:
[#<Nokogiri::XML::Element:0x4ea323e name="line" children=[#<Nokogiri::XML::Text:0x4ea294c "\n        (54) METHOD FOR FABRICATING A HIGH\n      ">]>,
 #<Nokogiri::XML::Element:0x4ea3018 name="line" children=[#<Nokogiri::XML::Text:0x4ea2654 "\n        DENSITY COMPOSITE MIM CAPACITOR\n      ">]>]

这应该可以解决问题。

答案 1 :(得分:0)

错误是错误:直接使用e.Salary作为整数值。

如果它是Nullable<int>,那么它将具有HasValue。但它改为int;并没有。