Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#mvc:将数据从视图持久化到控制器,然后通过单个对象持久化到列表<;ViewModel>;_C#_Asp.net Mvc 4_Model View Controller_Mvvm_Asp.net Mvc Viewmodel - Fatal编程技术网

c#mvc:将数据从视图持久化到控制器,然后通过单个对象持久化到列表<;ViewModel>;

c#mvc:将数据从视图持久化到控制器,然后通过单个对象持久化到列表<;ViewModel>;,c#,asp.net-mvc-4,model-view-controller,mvvm,asp.net-mvc-viewmodel,C#,Asp.net Mvc 4,Model View Controller,Mvvm,Asp.net Mvc Viewmodel,我正在使用一个视图,它将列表返回到控制器的创建操作方法 问题: public class UserViewModel { public string UserName { get; set; } public string Level { get; set; } public string Location { get; set; } public List<RoleViewModel> Roles { get; set; } } public cl

我正在使用一个
视图
,它将
列表
返回到
控制器
创建
操作方法

问题:

public class UserViewModel
{
    public string UserName { get; set; }
    public string Level { get; set; }
    public string Location { get; set; }
    public List<RoleViewModel> Roles { get; set; }
}

public class RoleViewModel
{
    public string RoleName{ get; set; }
    public bool IsSelected { get; set; }
}
[HttpPost]
    public ActionResult Create(List<UserViewModel> viewModelList)
    {
        for (int i= 0; i > viewModelList.Count; i++) {
            for (int j = 0; j > viewModelList[i].Roles.Count; j++) {

                    db.UserDetails.Add(new User 
                    {
                        username = viewModelList[i].UserName,
                        level = viewModelList[i].Level,
                        location = viewModelList[i].Location,
                        role = viewModelList[i].Roles[j].RoleName,
                        Approval = "",
                        Request = "",
                        Active = "Y"
                    });
            }

            db.SaveChanges();
            return RedirectToAction("Index","Users");
        }          
        return View(viewModelList); // not right
    }
  • 列表
    返回控制器是否正确?我这样做的原因是我必须为每个用户在数据库中输入多行内容。2.我的
    Controller
    实现不正确,因为我试图通过单用户对象添加数据,在该对象中,我收到了ViewModels列表,它只是单用户视图的表示形式。那么控制器应该如何实现呢
  • 我是否需要在控制器中使用
    public ActionResult Create()
    方法
  • 限制:

    public class UserViewModel
    {
        public string UserName { get; set; }
        public string Level { get; set; }
        public string Location { get; set; }
        public List<RoleViewModel> Roles { get; set; }
    }
    
    public class RoleViewModel
    {
        public string RoleName{ get; set; }
        public bool IsSelected { get; set; }
    }
    
    [HttpPost]
        public ActionResult Create(List<UserViewModel> viewModelList)
        {
            for (int i= 0; i > viewModelList.Count; i++) {
                for (int j = 0; j > viewModelList[i].Roles.Count; j++) {
    
                        db.UserDetails.Add(new User 
                        {
                            username = viewModelList[i].UserName,
                            level = viewModelList[i].Level,
                            location = viewModelList[i].Location,
                            role = viewModelList[i].Roles[j].RoleName,
                            Approval = "",
                            Request = "",
                            Active = "Y"
                        });
                }
    
                db.SaveChanges();
                return RedirectToAction("Index","Users");
            }          
            return View(viewModelList); // not right
        }
    
  • 无法更改数据库
  • 数据库中的用户或角色表都没有int type
    PK
  • 逻辑:

    public class UserViewModel
    {
        public string UserName { get; set; }
        public string Level { get; set; }
        public string Location { get; set; }
        public List<RoleViewModel> Roles { get; set; }
    }
    
    public class RoleViewModel
    {
        public string RoleName{ get; set; }
        public bool IsSelected { get; set; }
    }
    
    [HttpPost]
        public ActionResult Create(List<UserViewModel> viewModelList)
        {
            for (int i= 0; i > viewModelList.Count; i++) {
                for (int j = 0; j > viewModelList[i].Roles.Count; j++) {
    
                        db.UserDetails.Add(new User 
                        {
                            username = viewModelList[i].UserName,
                            level = viewModelList[i].Level,
                            location = viewModelList[i].Location,
                            role = viewModelList[i].Roles[j].RoleName,
                            Approval = "",
                            Request = "",
                            Active = "Y"
                        });
                }
    
                db.SaveChanges();
                return RedirectToAction("Index","Users");
            }          
            return View(viewModelList); // not right
        }
    
    用户可以为每个用户选择多个复选框(代表角色),这些复选框应作为单行保存在数据库中。比如说

    DB:dbo.User表在保存时应该有以下条目

    (abcdefg,级别A,位置B,角色2,空,空,Y)

    (msjdhcdu,levelA,locationB,Role2,null,null,Y)同一用户不同角色

    (msjdhcdu,levelA,locationB,Role3,null,null,Y)同一用户不同角色

    单击保存按钮并显示数据库中最近的更改后,用户将保持在同一页面(保存视图)

    视图模型:

    public class UserViewModel
    {
        public string UserName { get; set; }
        public string Level { get; set; }
        public string Location { get; set; }
        public List<RoleViewModel> Roles { get; set; }
    }
    
    public class RoleViewModel
    {
        public string RoleName{ get; set; }
        public bool IsSelected { get; set; }
    }
    
    [HttpPost]
        public ActionResult Create(List<UserViewModel> viewModelList)
        {
            for (int i= 0; i > viewModelList.Count; i++) {
                for (int j = 0; j > viewModelList[i].Roles.Count; j++) {
    
                        db.UserDetails.Add(new User 
                        {
                            username = viewModelList[i].UserName,
                            level = viewModelList[i].Level,
                            location = viewModelList[i].Location,
                            role = viewModelList[i].Roles[j].RoleName,
                            Approval = "",
                            Request = "",
                            Active = "Y"
                        });
                }
    
                db.SaveChanges();
                return RedirectToAction("Index","Users");
            }          
            return View(viewModelList); // not right
        }
    
    public类UserViewModel
    {
    公共字符串用户名{get;set;}
    公共字符串级别{get;set;}
    公共字符串位置{get;set;}
    公共列表角色{get;set;}
    }
    公共类角色视图模型
    {
    公共字符串RoleName{get;set;}
    公共布尔值被选为{get;set;}
    }
    
    视图:我可能在视图中做了一些非常错误的事情

    @model List<Project.ViewModels.UserViewModel>
    
    @using (@Html.BeginForm("Create", "User", FormMethod.Post ,new { id = "UserPermissionForm" }))
    
    
    {
    <table class="table">
        <tr>
            <th>User</th>
            @for (int i = 0; i < Model[0].Roles.Count; i++)
            {
                <th>
                    @Model[0].Roles[i].RoleName
                </th>
            }
        </tr>
        @for (int i = 0; i < Model.Count; i++)
        {
            <tr>
                <td>
                    @Html.HiddenFor(m => m[i].UserName)
                    @Model[i].UserName
                </td>
                @for (int j = 0; j < Model[i].Roles.Count; j++)
                {
                    <td>
                        @Html.CheckBoxFor(m => m[i].Roles[j].IsSelected)
                    </td>
                }
            </tr>
        }
    </table>
    
    <div class="form-actions">
            <button type="submit" class="btn btn-success submit" value="Save">Save changes</button>
        </div>
    
    <script>
        $('#Submit').click(function () {
            let url = '@Url.Action("Create", "Users")'
            $.post(url, $("#UserPermissionForm"))
            });
    </script>
    
    @型号列表
    @使用(@Html.BeginForm(“Create”,“User”,FormMethod.Post,new{id=“UserPermissionForm”}))
    {
    使用者
    @对于(int i=0;im[i].UserName)
    @型号[i]。用户名
    @对于(int j=0;jm[i].Roles[j].IsSelected)
    }
    }
    保存更改
    $(“#提交”)。单击(函数(){
    让url='@url.Action(“创建”、“用户”)'
    $.post(url,$(“#UserPermissionForm”))
    });
    
    控制器:

    public class UserViewModel
    {
        public string UserName { get; set; }
        public string Level { get; set; }
        public string Location { get; set; }
        public List<RoleViewModel> Roles { get; set; }
    }
    
    public class RoleViewModel
    {
        public string RoleName{ get; set; }
        public bool IsSelected { get; set; }
    }
    
    [HttpPost]
        public ActionResult Create(List<UserViewModel> viewModelList)
        {
            for (int i= 0; i > viewModelList.Count; i++) {
                for (int j = 0; j > viewModelList[i].Roles.Count; j++) {
    
                        db.UserDetails.Add(new User 
                        {
                            username = viewModelList[i].UserName,
                            level = viewModelList[i].Level,
                            location = viewModelList[i].Location,
                            role = viewModelList[i].Roles[j].RoleName,
                            Approval = "",
                            Request = "",
                            Active = "Y"
                        });
                }
    
                db.SaveChanges();
                return RedirectToAction("Index","Users");
            }          
            return View(viewModelList); // not right
        }
    
    [HttpPost]
    公共操作结果创建(列表视图模型列表)
    {
    对于(int i=0;i>viewModelList.Count;i++){
    对于(int j=0;j>viewModelList[i].Roles.Count;j++){
    db.UserDetails.Add(新用户
    {
    用户名=viewModelList[i]。用户名,
    级别=viewModelList[i]。级别,
    位置=viewModelList[i]。位置,
    role=viewModelList[i]。Roles[j]。RoleName,
    批准=”,
    请求“”,
    Active=“Y”
    });
    }
    db.SaveChanges();
    返回重定向操作(“索引”、“用户”);
    }          
    返回视图(viewModelList);//不正确
    }
    
    错误:

    传递到字典中的模型项的类型为“System.Collections.Generic.List`1[Project.ViewModels.UserViewModel]”,但此字典需要类型为“Project.Models.User”的模型项

    感谢您的指导/帮助

    EDIT1:已添加视图
    EDIT2:b添加了单体逻辑

    在这里,您将向视图发送一个模型:

    return View(viewModelList);
    
    并且该模型的类型为
    List
    。但是,根据错误,视图希望模型的类型为
    User
    。类似于视图本身的声明:

    @model Project.Models.User
    
    所以你有两个选择:

  • 更改视图以接受类型为<代码>列表的模型,并在视图本身内进行必要的更改以使用该新模型类型
  • 向视图发送类型为
    Project.Models.User
    (这可能是
    列表中一个元素的转换)的模型
  • 在技术层面上,任何一个选项都是完全有效的。(虽然两者相互排斥,但它是一个或另一个。)取决于您所构建的系统中哪个选项更有意义,该视图是否应该在实例或列表上运行


    编辑:您对问题的编辑(包括视图)似乎表明您可能看到了错误的视图。您向我们显示的视图与您向我们显示的错误消息不匹配。请记住,控制器方法的两个退出路径(重定向和返回视图)可能会调用不同的视图


    无论如何,问题的核心仍然存在。在某个地方,您提供的视图的类型与预期的不同。您可能需要进行一些额外的调试,以确定代码中发生的情况,但最终问题和潜在的解决方案仍然与上述相同。

    请显示您的视图页面。您在其中定义
    @model
    查看现在添加的页面详细信息。@sharlene:鉴于添加到问题中的视图,这不再有意义。错误表示视图需要类型为
    Project.Models.User
    的模型。此错误具体发生在哪里?