映射函数绑定指定函数但不映射以下参数:

时间:2016-01-25 21:52:44

标签: c# asp.net asp.net-mvc

我有一个MVC 5数据库第一个Entity Framework 6应用程序,它有多个最初隐藏的字段,但可能会根据下拉列表中选择的角色显示。这用于创建系统的新管理用户。

模型是由模板创建的,并没有调用这些隐藏字段,我使用存储过程保存数据,以便它可以插入多个表而不是直接插入到单个表中。

我看到的错误消息说映射函数绑定指定函数NewAMSModel.Store.usp_AddAdminUser但不映射以下参数:DivisionID,DistrictID,DepartmentID,RegionID,ZoneID,LocationID。

以下是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方式。

1 个答案:

答案 0 :(得分:0)

如果您使用@Html.Hidden@Html.HiddenFor为普通用户呈现它们,那么它仍会发布(但仍然可以由精明的用户进行编辑)。

这与在原始HTML术语中使用<input type="hidden" name="someName" value="someValue">相同。

最安全的事情是不向那些不应该看到它的人展示价值,在记忆中记住它(例如Session,一个cookie)。这取决于你需要多么安全。

最不安全的是在隐藏的div中使用标准html输入,在CSS上设置display: none,但仍应回发。