在我的viewmodel中,还有其他一些属性,我有这个:
public int AuthorStatus {get; set;}
public string AuthorStatusLabelClass
{
get
{
switch (AuthorStatus)
{
case 1:
return "label-success";
case 0:
return "label-danger";
case 2:
return "label-warning";
default:
return "label-default";
}
}
set
{
}
}
public IQueryable<VM_Authors> AuthorList { get; set; }
如何访问控制器中的AuthorStatusLabelClass;
var vm = new ViewModels.VM_Authors();
vm.Categories = new SelectList(db.AuthorCategories, "CategoryID", "CategoryName");
vm.AuthorList = (from a in db.Authors
select new ViewModels.VM_Authors
{
FirstName = a.FirstName,
LastName = a.LastName,
Email = a.Email,
AuthorStatusLabelClass = ####HOW_SHOULD_I_RETRIEVE_IT_HERE####
这样我就可以在我的视图中使用它:
@foreach (var item in Model.AuthorList)
{
<tr>
<td><span class="label @item.AuthorStatusLabelClass">@item.AuthorStatus</span></td>
<td><img src="" class="img-circle" /></td>
<td><span class="text-semibold">@item.FirstName @item.LastName</span></td>
<td>@item.SelectedCategoryID</td>
<td></td>
</tr>
}
可能是我的整个方法有所不同?任何帮助将不胜感激!
答案 0 :(得分:1)
修改强>
由于您提到AuthorStatusLabelClass
不是数据库表的列和之一,它完全取决于您的AuthorStatus
,因此您不需要完全分配它。只需:
select new ViewModels.VM_Authors
{
FirstName = a.FirstName,
LastName = a.LastName,
Email = a.Email,
AuthorStatus = a.AuthorStatus //assign this
当您在AuthorStatusLabelClass
中获得View
时,只需直接获取该AuthorStatus
,并且上面指定的AuthorStatusLabelClass
将会在View
中归还setter
{1}}。
<强>原始强>
您似乎未在AuthorStatusLabelClass
中设置select new ViewModels.VM_Authors
{
FirstName = a.FirstName,
LastName = a.LastName,
Email = a.Email,
AuthorStatusLabelClass = //This is a set ####HOW_SHOULD_I_RETRIEVE_IT_HERE####
,因此您无法执行此操作:
AuthorStatusLabelClass
带有setter
的 private string _authorStatusLabelClass = "";
public string AuthorStatusLabelClass
{
get
{
switch (AuthorStatus)
{
case 1:
_authorStatusLabelClass = "label-success";
case 0:
_authorStatusLabelClass = "label-danger";
case 2:
_authorStatusLabelClass = "label-warning";
default:
_authorStatusLabelClass = "label-default";
}
return _authorStatusLabelClass;
}
set
{
_authorStatusLabelClass = value;
}
}
可能如下所示:
AuthorStatus
此外,由于您需要先将AuthorStatusLabelClass
设置为某个号码,然后才能检索AuthorStatusLabelClass
,因此如果没有AuthorStatus
,您可能无法正确获取AuthorStatusLabelClass
}。课程indexer
中您可能需要的是public AuthorStatusLabelClass this[int index] { //thus you could give index as an input for this instance
get {
switch (index)
{
case 1:
return "label-success";
case 0:
return "label-danger";
case 2:
return "label-warning";
default:
return "label-default";
}
}
}
:
vm.AuthorList = (from a in db.Authors
select new ViewModels.VM_Authors
{
FirstName = a.FirstName,
LastName = a.LastName,
Email = a.Email,
AuthorStatusLabelClass = a.AuthorStatusLabelClass[a.AuthorStatus]
并像这样使用它:
public wizard()
{
InitializeComponent();
var intro = new intro();
wizardframe.NavigationService.Navigate(intro);
} // this is the intro page.
private void ileri_b1_Click(object sender, RoutedEventArgs e)
{
string introtest = "intro";
if (introtest == "intro")
{
var wpage2 = new wpage2();
wizardframe.NavigationService.Navigate(wpage2);
introtest = "wpage2";
}
else if (introtest == "wpage2")
{
string fileName = String.Format(@"{0}\temp\aciklama.txt", Environment.CurrentDirectory);
StreamWriter file = new StreamWriter(fileName);
file.Write(attext);
file.Close(); // this is the "Ileri" Button click event.Just saving file.
}
答案 1 :(得分:0)
我可以看到你的AuthorStatusLabelClass
有吸气剂和一个空的二传手。 Getter依赖于AuthorStatus
属性。您所要做的就是正确设置AuthorStatus
属性。
select new ViewModels.VM_Authors
{
FirstName = a.FirstName,
LastName = a.LastName,
Email = a.Email,
AuthorStatus = ### SETUP status and don't worry about AuthorStatusLabelClass ###