我一直在互联网上搜索关于MVC的多对多关系(并且已经在这里阅读了关于它的所有内容),但我似乎无法解决我的问题。
我在客户端和标准服务之间有多对多的关系,如下所示。
public class Client
{
public Client()
{
CustomServices = new HashSet<CustomService>();
StandardServices = new HashSet<StandardService>();
}
public int ClientID { get; set; }
public string Name { get; set; }
public Nullable<int> Users { get; set; }
public Nullable<bool> FH { get; set; }
public Nullable<bool> PH { get; set; }
public bool Hosted { get; set; }
public string ShortName { get; set; }
public Nullable<short> AttachmentLimit { get; set; }
public virtual ICollection<CustomService> CustomServices { get; set; }
public virtual ICollection<StandardService> StandardServices { get; set; }
标准服务。
public class StandardService
{
public StandardService()
{
Clients = new HashSet<Client>();
}
public int StandardServiceID { get; set; }
public string StandardServiceName { get; set; }
public string LogOnAs { get; set; }
public int ServerID { get; set; }
public virtual Server Server { get; set; }
public virtual ICollection<Client> Clients { get; set; }
创建新客户端时,我希望能够从客户端使用的标准服务的复选框列表中进行选择。我可以使用viewbag让它们显示在创建视图中,但希望能够使用视图模型。
我还想知道如何使用默认模型绑定发布此内容。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ClientID,Name,Users,FH,Hosted,PH,ShortName,AttachmentLimit")] Client client)
{
if (ModelState.IsValid)
{
db.Clients.Add(client);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(client);
}
以下是客户端控制器的创建方法。
public ActionResult Create()
{
ViewBag.StandardServices = db.StandardServices.ToList();
return View();
}
我的创建视图的一部分位于下方(复选框)。
<div class="form-group">
@Html.LabelFor(model => model.StandardServices, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@foreach (var StandardService in ViewBag.StandardServices)
{
<div class="checkbox">
<input class="checkbox" type="checkbox" name="StandardServiceID" id="StandardServiceID" value="@StandardService.StandardServiceID"/>
@StandardService.StandardServiceName<br>
</div>
}
</div>
</div>
那我怎么能这样做呢?我尝试过很多东西,但是不想把它们全部列出来。没有工作就足够了。任何帮助都将不胜感激。我不需要完整的解决方案,只需要一些关于去哪里的建议。谢谢!
基于以下评论;并查看提供的链接,我想我有部分解决方案。我现在只需要处理这个帖子。以下是我对视图模型的看法。
public class CreateClientVM
{
public CreateClientVM()
{
ClientStandardServices = new List<StandardService>();
}
public int ClientID { get; set; }
public string Name { get; set; }
public Nullable<int> Users { get; set; }
public Nullable<bool> FH { get; set; }
public Nullable<bool> PH { get; set; }
public bool Hosted { get; set; }
public string ShortName { get; set; }
public Nullable<short> AttachmentLimit { get; set; }
public List<StandardService> ClientStandardServices { get; set; }
public List<StandardServiceVM> StandardServices { get; set; }
}
public class StandardServiceVM
{
public int StandardServiceID { get; set; }
public string StandardServiceName { get; set; }
public bool IsSelected { get; set; }
}
以下是我的客户端控制器创建方法。
public ActionResult Create()
{
//ViewBag.StandardServices = db.StandardServices.ToList();
CreateClientVM clientVM = new CreateClientVM();
var ListVM = new List<StandardServiceVM>();
var standardServices = db.StandardServices.ToList();
foreach(var s in standardServices)
{
var viewModel = new StandardServiceVM
{
StandardServiceID = s.StandardServiceID,
StandardServiceName = s.StandardServiceName,
IsSelected = false
};
ListVM.Add(viewModel);
}
clientVM.StandardServices = ListVM;
return View(clientVM);
}
最后是我创建视图的一部分。
<div class="form-group">
@Html.HiddenFor(m => m.ClientID)
@Html.LabelFor(m => m.StandardServices, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@for (int i = 0; i < Model.StandardServices.Count; i++)
{
<div class="checkbox">
@Html.HiddenFor(m => m.StandardServices[i].StandardServiceID)
@Html.CheckBoxFor(m => m.StandardServices[i].IsSelected)
@Html.LabelFor(m => m.StandardServices[i].IsSelected, Model.StandardServices[i].StandardServiceName)
</div>
}
</div>
</div>
以下是查看页面源时呈现的html。我认为这看起来是正确的,但我并不完全确定。
<div class="form-group">
<input data-val="true" data-val-number="The field ClientID must be a number." data-val-required="The ClientID field is required." id="ClientID" name="ClientID" type="hidden" value="0" />
<label class="control-label col-md-2" for="StandardServices">StandardServices</label>
<div class="col-md-10">
<div class="checkbox">
<input data-val="true" data-val-number="The field StandardServiceID must be a number." data-val-required="The StandardServiceID field is required." id="StandardServices_0__StandardServiceID" name="StandardServices[0].StandardServiceID" type="hidden" value="1" />
<input data-val="true" data-val-required="The IsSelected field is required." id="StandardServices_0__IsSelected" name="StandardServices[0].IsSelected" type="checkbox" value="true" />
<input name="StandardServices[0].IsSelected" type="hidden" value="false" />
<label for="StandardServices_0__IsSelected">FHEmailService</label>
</div>
<div class="checkbox">
<input data-val="true" data-val-number="The field StandardServiceID must be a number." data-val-required="The StandardServiceID field is required." id="StandardServices_1__StandardServiceID" name="StandardServices[1].StandardServiceID" type="hidden" value="2" />
<input data-val="true" data-val-required="The IsSelected field is required." id="StandardServices_1__IsSelected" name="StandardServices[1].IsSelected" type="checkbox" value="true" />
<input name="StandardServices[1].IsSelected" type="hidden" value="false" />
<label for="StandardServices_1__IsSelected">FHScheduledReports</label>
</div>
</div>
</div>
如果这看起来不错,有人会发表评论吗?我相信它确实如此。现在我只需要弄清楚如何发布它。