简体   繁体   English

“int”不包含“HasValue”的定义?

[英]'int' does not contain a definition for 'HasValue'?

I am having trouble finding out what the error is with my code.我无法找出我的代码有什么错误。 It says :它说 :

'int' does not contain a definition for 'HasValue' and no extension method 'HasValue' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?) “int”不包含“HasValue”的定义,并且找不到接受“int”类型的第一个参数的扩展方法“HasValue”(您是否缺少 using 指令或程序集引用?)

I checked my code and its correct but I can't find out where the error is coming from.我检查了我的代码及其正确性,但我无法找出错误的来源。 Here is the file这是文件

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;
            }
        }
    }
}

It's the line这是线

e.Salary.HasValue

As mentioned in the previous post e.Salary is an integer value type and cannot be null.如上一篇文章所述,e.Salary 是一个整数值类型,不能为空。 Integer types will be 0 be default.整数类型将默认为 0。

You could change your if statement to:您可以将 if 语句更改为:

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

this should do the trick.这应该可以解决问题。

Error is what error says: use e.Salary as an integer value directly.错误就是错误所说的:直接使用e.Salary作为整数值。

If it was a Nullable<int> then it would have a HasValue.如果它是一个Nullable<int>那么它会有一个 HasValue。 But it is an int instead;但它是一个int and does not.并且没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM