更新时间:2023-02-25 19:14:59
首先,我不会尝试将 IQueryable 用于 Keywords
属性,***使用 IList.其次,将关键字和 CheckboxList 组合成一个属性可能会更好,让您的关键字类是这样的:
Firstly I would not attempt to use IQueryable for your Keywords
property, better would be to use IList. Secondly it would probably be better to combine Keywords and CheckboxList into a single property, having your keyword class something like this:
public class Keyword
{
public int KeywordId {get;set;}
public bool Checked {get;set;}
}
所以 ThesisWithKeywordsModel
看起来像这样:
So ThesisWithKeywordsModel
would look like this:
public class ThesisWithKeywordsModel
{
public Thesis thesis { get; set; }
public IList<Keyword> keywords { get; set; }
}
然后在您的控制器中,您应该能够:
Then in your controller, you should be able to do:
thesis.keywords = db.KeywordRepository.GetAllKeywords()
.Select(kw => new Keyword
{
KeyeordId = kw.KeywordId,
Checked = false
}).ToList();
接下来,当将值列表传入/传出视图时,您需要将 cshtml 中的 foreach
循环更改为 for
循环.
Next you need to change your foreach
loop in the cshtml into a for
loop when passing a list of values to/from the view.
for(var i = 0; i < Model.keywords.Count; i++)
{
<input type="checkbox" id="@Model.keywords[i].KeywordId" name="checkboxList" checked='@Model.keywords[i].Checked ? "checked" : ""'>
<label for="@Model.keywords[i].KeywordId">@Model.keywords[i].Value</label><br/>
}