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
}
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
的模型。此错误具体发生在哪里?