我有一个扩展IdentityRole类的类:
public class Subscription : IdentityRole
{
//parameterless construcor required by EF
public Subscription() { }
public Subscription(string roleName) : base(roleName)
{
}
public string SubscriptionID { get; set; }
public override string Name { get; set; }
public DateTime? Expiration { get; set; }
}
在AppUser
扩展IdenityUser
public class AppUser : IdentityUser
{
public Subscription UserSubscription { get; set; } = new Subscription();
}
类中使用
Subscription
在我的控制器中,我使用UserSubscription
类中的方法设置model.Subsubscription
属性,其中//it's been verified that the user properties are correctly set here
user.UserSubscription.SetSubscription(user.UserSubscription, model.Subscription);
return RedirectToAction("IndexUser");
是一个字符串
user.UserSubscription.Name
但是,当我呈现以下视图时,@{
ViewBag.Title = "User Accounts";
Layout = "_AdminLayout";
}
@model IEnumerable<AppUser>
<div class="bg-primary panel-body"><h4>@ViewBag.Title</h4></div>
<table class="table table-condensed table-bordered">
<tr><th>ID</th><th>Name</th><th>Subscription Type</th><th>Email</th></tr>
@if (Model.Count() == 0)
{
<tr><td colspan="3" class="text-center">No User Accounts</td></tr>
}
else
{
foreach (AppUser user in Model)
{
<tr>
<td>@user.Id</td><td>@user.UserName</td><td>@user.UserSubscription.Name</td><td>@user.Email</td>
<td>
<form asp-action="DeleteUser" asp-route-id="@user.Id" method="post">
<a class="btn btn-sm btn-primary" asp-action="EditUser" asp-route-id="@user.Id">Edit</a>
<button type="submit" class="btn btn-sm btn-danger">Delete</button>
</form>
</td>
</tr>
}
}
</table>
<a class="btn btn-primary" asp-action="CreateUser">Create</a>
属性为空
user.UserSubscription.Name
在视图中,除AppUser
外,所有其他属性都正确显示。为什么这些数据没有传输到视图?我觉得这可能是由于UserSubscription
班级public void SetSubscription(Subscription subscription, string subType)
{
var subscriptionType = subType.ToLower();
if (subscriptionType == "t0")
{
subscription.Name = "T0";
subscription.Expiration = null;
}
else if (subscriptionType == "t1")
{
subscription.Name = "T1";
subscription.Expiration = DateTime.Now.AddYears(1);
}
else if (subscriptionType == "t2")
{
subscription.Name = "T2";
subscription.Expiration = DateTime.Now.AddYears(1);
}
else if (subscriptionType == "t3")
{
subscription.Name = "T3";
subscription.Expiration = DateTime.Now.AddYears(1);
}
}
属性造成的,但我不确定如何正确设置
编辑:共享SetSubscription方法
userManager.Users
虽然在重定向之前的CreateUser操作方法中似乎是正确的,但是当通过public IActionResult IndexUser() => View(userManager.Users);
传递时,UserSubscription Name属性为空
IdentityRole
我能够通过删除扩展AppUser
并扩展public class AppUser : IdentityUser
{
public string Substription { get; set; }
public DateTime? Expiration { get; set; }
}
类
@user.Subscription
然后我使用类似的SetSubscription()方法设置AppUser属性并将视图更改为显示SELECT CELLS.ID, CELLS.NAME, CELLS.NUM, CELLS.AC_ID, AC.SERIAL,AC.FILE_NAME, AC.FILE_DATA
FROM CELLS
LEFT JOIN AC
ON CELLS.AC_ID = AC.ID
WHERE CELLS.AC_ID IS NOT NULL
ORDER BY CELLS.NUM
并且它有效。这有简化我的控制器代码的附带好处。我剩下的问题是:扩展IdentityUser类而不是IdentityRole类是否会阻止我以后可能需要的某些功能?