我有一个 MVC 5 数据库第一个实体框架 6 应用程序,该应用程序具有多个最初隐藏的字段,但可能会根据下拉列表中选择的角色显示。 这是为了创建系统的新管理用户。
该模型是由模板创建的,不调用这些隐藏字段,我使用存储过程保存数据,以便它可以插入到多个表中,而不是直接插入到单个表中。
我看到的错误消息说映射函数绑定指定函数NewAMSModel.Store.usp_AddAdminUser但不映射以下参数:部门 ID、地区 ID、部门 ID、区域 ID、区域 ID、位置 ID。
这是Visual Studio创建的模型。
namespace NewAMS.Models
{
using System;
using System.Collections.Generic;
public partial class AdminUser
{
public AdminUser()
{
this.Departments = new HashSet<Department>();
this.Districts = new HashSet<District>();
this.Divisions = new HashSet<Division>();
this.Locations = new HashSet<Location>();
this.Regions = new HashSet<Region>();
this.Zones = new HashSet<Zone>();
}
public string AdminID { get; set; }
public string PersonName { get; set; }
public string Email { get; set; }
public byte RoleID { get; set; }
public bool ReceiveNotifications { get; set; }
public bool ChangePW { get; set; }
public bool Deactivated { get; set; }
public virtual AdminLogin AdminLogin { get; set; }
public virtual Role Role { get; set; }
public virtual ICollection<Department> Departments { get; set; }
public virtual ICollection<District> Districts { get; set; }
public virtual ICollection<Division> Divisions { get; set; }
public virtual ICollection<Location> Locations { get; set; }
public virtual ICollection<Region> Regions { get; set; }
public virtual ICollection<Zone> Zones { get; set; }
}
}
列为 ICollection 的所有属性都是可选字段的外键,应在创建页上分配。 所以我向模型添加了以下属性
public byte DivisionID { get; set; }
public byte DistrictID { get; set; }
public byte DepartmentID { get; set; }
public byte RegionID { get; set; }
public byte ZoneID { get; set; }
public byte LocationID { get; set; }
但是,每次刷新数据库时,模板都会删除添加的部件。 当我尝试映射过程时,它会给我错误消息。
这是我的存储过程
CREATE PROCEDURE usp_AddAdminUser
@AdminID nvarchar(15),
@PersonName nvarchar(50),
@Email nvarchar(50),
@RoleID tinyint,
@ReceiveNotifications bit,
@DivisionID int = NULL,
@DistrictID int = NULL,
@DepartmentID int = NULL,
@RegionID int = NULL,
@ZoneID int = NULL,
@LocationID int = NULL
AS
SET NOCOUNT ON
DECLARE @Message varchar(100)
IF NOT EXISTS(SELECT PersonName FROM dbo.AdminUsers WHERE AdminID = @AdminID)
BEGIN
INSERT
dbo.AdminUsers
VALUES
(
@AdminID,
@PersonName,
@Email,
@RoleID,
@ReceiveNotifications,
0,
0
)
--Depending on the role assigned create the user record in the foreign key table(s)
IF @RoleID = 4 --Division Managers
BEGIN
INSERT
dbo.DivisionManager
VALUES
(
@AdminID,
@DivisionID
)
END
IF @RoleID = 5 -- District Managers
BEGIN
INSERT
dbo.DistrictManager
VALUES
(
@AdminID,
@DistrictID
)
END
IF @RoleID = 6 -- Regional Managers
BEGIN
INSERT
dbo.RegionManager
VALUES
(
@AdminID,
@RegionID
)
END
IF @RoleID = 7 -- Zone Managers
BEGIN
INSERT
dbo.ZoneManager
VALUES
(
@AdminID,
@ZoneID
)
END
IF @RoleID = 8 -- Department Managers
BEGIN
INSERT
dbo.DepartmentManager
VALUES
(
@AdminID,
@DepartmentID
)
END
IF @RoleID = 9 -- Department Manager at a specific location
BEGIN
INSERT
dbo.DepartmentManager
VALUES
(
@AdminID,
@DepartmentID
)
INSERT
dbo.Manager_Locations
VALUES
(
@AdminID,
@LocationID
)
END
IF @RoleID = 10 -- location managers
BEGIN
INSERT
dbo.Manager_Locations
VALUES
(
@AdminID,
@LocationID
)
END
-- Now build the password
DECLARE @PwdWithSalt nvarchar(60)
DECLARE @salt UNIQUEIDENTIFIER=NEWID()
DECLARE @Len int
DECLARE @Min tinyint
DECLARE @Range tinyint
DECLARE @Exclude varchar(50)
DECLARE @Char char
DECLARE @Password nvarchar(20)
SET @Len = 12
SET @Min = 35
SET @Range = 74
SET @Exclude = '0:;`0l1-<>/[]()'''
SET @Password = ''
-- Create a temporary password
WHILE @Len > 0
BEGIN
SELECT @Char = char(ROUND(RAND() * @Range + @Min,0))
IF CHARINDEX(@Char,@Exclude) = 0
BEGIN
SET @Password = @Password + @Char
SET @Len = @Len -1
END
END
SET @PwdWithSalt = @Password + CAST(@salt as nvarchar(36))
INSERT
dbo.AdminLogins
VALUES
(
@AdminID,
HASHBYTES('SHA1',@PwdWithSalt),
@salt
)
SET @Message = 'here is your temporary password ' + @Password
END
Exec msdb.dbo.sp_send_dbmail @profile_name='email',
@recipients= @Email,
@subject='Your account has been created',
@body=@Message
希望这能解释我想要完成的目标。 我尝试在不使用模板的情况下创建新模型、控制器和视图,但该模板不起作用。如果不能在一个过程中完成,我不介意分多个步骤完成,只是需要一些关于如何以 asp.net-mvc 方式完成此操作的帮助。
如果您使用@Html.Hidden
或@Html.HiddenFor
为普通用户渲染它们,那么它仍然会发布它们(但仍然可以由精明的用户编辑)。
这与在原始 HTML 术语中使用<input type="hidden" name="someName" value="someValue">
相同。
最安全的做法是不要向那些不应该看到它的人显示这些值,而是在内存中记住它(例如 Session
,饼干)。这取决于您需要有多安全。
最不安全的是在一个隐藏的div中使用标准的html输入,display: none
设置在CSS上,但这仍然应该回发。