问题描述
所以我试图用[Slauma的答案] [1]
是我迄今所做的,
型号 ViewModelProspectUsers 的
公众诠释标识{搞定;组; }
公共字符串用户{搞定;组; }
公共IEnumerable的< ViewModelUserProspectSelect>展望{搞定;组; }
型号 ViewModelUserProspectSelect 的
公众诠释ProspectID {搞定;组; }
公共字符串名称{;组; }
公共BOOL IsSelected {搞定;组; }
查看 UserInProspect 的
@model OG.ModelView.ViewModelProspectUsers@using(Html.BeginForm())
{< DIV CLASS =容器>
< DIV CLASS =contentContainer>
@ Html.HiddenFor(型号=> model.Id)
展望用户和LT; H3> @ Html.DisplayTextFor(型号=> model.User)LT; / H3 GT&;
< / DIV>
< DIV CLASS =contentContainer2>
< H5>请选择您的前景要分配给该用户< / H5>
&所述p为H.;&下; / P> @ Html.EditorFor(型号=> model.Prospects) < / DIV>
<输入类型=提交值=保存更改/>
@ Html.ActionLink(取消,索引)
< / DIV>
}
编辑器查看位于下ChangeUserInfo / EditorTemplates / * _ * ViewModelUserprospectSelect.cshtml
@model OG.ModelView.ViewModelUserProspectSelect@ Html.HiddenFor(型号=> model.ProspectID)
@ Html.HiddenFor(型号=> model.Name)
测试
@ Html.LabelFor(型号=> model.IsSelected,Model.Name)
@ Html.EditorFor(型号=> model.IsSelected)
[获取] 方法的 UserInProspect 的操作
公众的ActionResult UsersInProspect(INT ID = 0)
{
VAR数据= db.UserProfiles
。凡(S => s.UserID == id)的
。选择(S = GT;新
{
视图模型=新ViewModelProspectUsers
{
ID = s.UserID,
用户= s.UserName
},
前景= s.Prospects.Select(C => c.ProspectID)
})
.SingleOrDefault(); 如果(数据== NULL)
返回HttpNotFound(); //将数据库的所有公司
data.ViewModel.Prospects = db.Prospect
。选择(C =>新建ViewModelUserProspectSelect
{
ProspectID = c.ProspectID,
名称= c.ProspectName
})
.ToList(); //设置IsSelected标志:真(=复选框选中),如果该公司
//已经与订阅相关;假,如果不
的foreach(在data.ViewModel.Prospects变种C)
c.IsSelected = data.prospects.Contains(c.ProspectID); 返回查看(data.ViewModel); }
[HttpPost] 方法的 UserInProspect 的操作
公众的ActionResult UsersInProspect(ViewModelProspectUsers视图模型)
{
如果(ModelState.IsValid)
{
VAR订阅= db.UserProfiles.Include(S = GT; s.Prospects)
.SingleOrDefault(S => s.UserID == viewModel.Id); 如果(订阅!= NULL)
{
//更新标量的属性,如金额
//subscription.Prospects = viewModel.Prospects;
//订阅。 =订阅。
//列表<串GT; myList中=新名单<串GT;();
// myList中= viewModel.Prospects.Cast<串GT;()了ToList()。 // IEnumerable的< dbProspect> Isubscription = subscription.Prospects;
////或显式:
// VAR iPersonList =(IEnumerable的< dbProspect>)myList中;
//或多个标量属性更通用
// _context.Entry(订阅).CurrentValues.SetValues(视图模型);
//但是如果你使用相同的密钥属性名这只会工作
//在视图模型和实体 的foreach(在viewModel.Prospects VAR前景)
{
如果(prospect.IsSelected)
{
如果(!subscription.Prospects.Any(
C => c.ProspectID == prospect.ProspectID))
{
//如果公司被选中,但尚未
//在DB相关,加关系
VAR addedProspect =新dbProspect {ProspectID = prospect.ProspectID};
db.Prospect.Attach(addedProspect);
subscription.Prospects.Add(addedProspect);
}
}
其他
{
VAR removedProspect = subscription.Prospects
.SingleOrDefault(C => c.ProspectID == prospect.ProspectID);
如果(removedProspect!= NULL)
//如果公司没有选择,但目前
//在DB的关系,删除关系
subscription.Prospects.Remove(removedProspect);
}
} db.SaveChanges();
} 返回RedirectToAction(「指数」);
} 返回视图(视图模型);
}
Remove that again. "EditorTemplates" should not be a namespace.
An "editor template" isn't a C# (.cs
) code file but a Razor view (.cshtml
). Move the file ViewModelUserProspectSelect.cs
to the folder where also ViewModelProspectUsers.cs
is and change its namespace to the same for both classes (OG.Models
).
(Why is there a subfolder /Info/
in the path??? Or is it a typo and just Views/ChangeUsersInfo/EditorTemplates
is meant? I assume that the controller has the name ChangeUsersInfoController
, right?)
Then create a new file ViewModelUserProspectSelect.cshtml
in the Views/ChangeUsersInfo/EditorTemplates
folder that contains the view from the other answer, this one:
@model OG.Models.ViewModelUserProspectSelect
@Html.HiddenFor(model => model.ProspectID)
@Html.HiddenFor(model => model.Name)
@Html.LabelFor(model => model.IsSelected, Model.Name)
@Html.EditorFor(model => model.IsSelected)
And the contentContainer2
div element in your main view should look like this:
<div class="contentContainer2">
<h5>Please Select Prospects you wish to assign to this User.</h5>
@Html.EditorFor(model => model.Propects)
</div>
这篇关于保存到m-2-m的具有复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!