无法从视图获取值

时间:2019-06-20 06:57:43

标签: c# asp.net asp.net-mvc repository-pattern unit-of-work

我正在了解工作单元,存储库和服务。现在我不知道为什么在登录屏幕时输入的值似乎为空

在我提交时输入值后,在值屏幕中仅显示空值

当控制器中的断点时,我发现其中为空。

控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model;
using ClassLibrary1;
namespace Registeration.Controllers
{
    public class DbController : Controller
    {
        // GET: Db
        public ActionResult Index()
        {
            Service obj = new Service();
            var lis = obj.GetAllLogins();
            return View(lis);
        }
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(Mdl Obj)
        {
            Service obj1 = new Service();
            var details = obj1.CreateEmp(Obj);
            return RedirectToAction("Index");
        }
        public ActionResult Login()

        {
            return View();
        }
        [HttpPost]
        public ActionResult Login(string userName, string dpt)
        {
            Service obj = new Service();
            var res = obj.Login(userName, dpt); //values shows null
            if (res != null)
            {
                return RedirectToAction("Index");
            }
            return RedirectToAction("Login");
        }
    }
}

服务代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using DataAccess;
using DataAccess.UoW;
using Model;

namespace ClassLibrary1
{
    public class Service
    {
        private readonly Unit _uow;
        public Service()
        {
            _uow = new Unit();
        }
        public IEnumerable<Mdl> GetAllLogins()
        {
            var logins = _uow.Register.GetAll().ToList();
            if (logins.Any())
            {
                var config = new MapperConfiguration(cfg =>
                {
                    cfg.CreateMap<Table_1, Mdl>();
                });

                IMapper mapper = config.CreateMapper();
                var dest = mapper.Map<List<Table_1>, List<Mdl>>(logins);
                return dest;
            }
            return null;
        }
        public Mdl Login(string userName,string dpt)
        {
            var logins = _uow.Register.Get(x=>x.Name==userName && x.Deprmt==dpt);
            if (logins != null)
            {
                var obj1 = new Mdl();
                if (logins.Id > 0)
                    return obj1;
            }
            return null;
        }
    }
}

型号:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
    public class Mdl
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Sex { get; set; }
        public string Deprmt { get; set; }
        public string State { get; set; }
    }
}

1 个答案:

答案 0 :(得分:0)

您尚未在此处显示查看代码,如果在login [POST]方法上进行了查看,则可能未在html控件中使用name属性。

控件应与您的参数名称相同(用户名,dpt),以便ModelBinder可以绑定它们。