如何比较数据库和客户端.Net Core 2中的值 [英] How to compare the value between the one from database and the client .Net Core 2
问题描述
我们有此编辑Razor页面(edit.cshtml),它是从以下页面模型扩展而来的,它非常基本,仅包括PopulateRolesDropDownList:
We have this edit Razor pages (edit.cshtml) which is extended from the following page model and it's very basic only include the PopulateRolesDropDownList:
public class RoleNamePageModel : PageModel
{
public SelectList RoleNameSL { get; set; }
public void PopulateRolesDropDownList(ApplicationDbContext _context,
object selectedRole = null)
{
var rolesQuery = from d in _context.Roles
orderby d.Name // Sort by name.
select d;
RoleNameSL = new SelectList(rolesQuery,
"RoleId", "Name", selectedRole);
}
}
在此编辑页面中,我们添加:
Also in this Edit page, we added:
<input type="hidden" asp-for="User.UserRoles.ElementAt(0).RoleId" name="User.Current.RoleId" />
我们还在后面的代码中执行了[BindProperty]
We also do the [BindProperty] in the code behind
public ApplicationUser User { get; set; }
我们需要找出此模型是否有变化。这样做的方法是什么?
We need to find out whether there is a change on this model. What is the approach to do this?
环境:
- .NET Core 2.2
- 剃刀页
更新-1 :
在PostAsync上,我们再次调用了数据库:
On the PostAsync, we made another call to the database:
var userRoleToUpdate = await _context.UserRoles
.FirstOrDefaultAsync(m => m.UserId == id.ToString());
我们只需要将此值与下拉列表中的更改进行比较即可。我们无法工作。
We just need to compare this value with the change on a drop-down list or not. We could not work how.
更新-2:
我们确实做了更改按照@NevilleNazerane的建议,如下:
We did change as per recommend by @NevilleNazerane below:
public class AssignClubUserViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string UserName { get; set; }
public Guid SelectedRoleID { get; set; }
}
[BindProperty]
public AssignClubUserViewModel AssignClubUser { get; set; }
并添加OnGetAsync:
and added OnGetAsync:
public async Task<IActionResult> OnGetAsync(Guid? id)
{
if (id == null)
return NotFound();
var user = await _context.Users
.Include(u => u.ClubApplicationUsers)
.FirstOrDefaultAsync(m => m.Id == id.ToString());
AssignClubUser.FirstName = user.FirstName;
AssignClubUser.LastName = user.LastName;
AssignClubUser.UserName = user.UserName;
AssignClubUser.SelectedClubID =
user.ClubApplicationUsers.ElementAt(0).ClubID;
....
这是对的吗?我收到错误:NullReferenceException:AssignClubUser.FirstName行上的Arg_NullReferenceException;
Is this right? I got the error: NullReferenceException: Arg_NullReferenceException on line AssignClubUser.FirstName = user.FirstName;
更新-3:
通过创建ModemView然后在OnGetAsync()上进行查询来固定以确保使用ModelView进行映射:
Fixed by creating a ModemView and then on the OnGetAsync() for query ensure to mapped with the ModelView:
var user = await _context.Users
.Include(u => u.ClubApplicationUsers)
.Where(t => t.Id == id.ToString())
.Select(t => new AssignClubUserViewModel<ApplicationUser>
{
FirstName = t.FirstName,
LastName = t.LastName,
UserName = t.UserName,
SelectedClubID = t.ClubApplicationUsers.ElementAt(0).ClubID
}).SingleAsync();
推荐答案
由于您拥有视图模型,因此建议您简化您的绑定,并让您的背后代码处理其他功能。您可以先创建 SelectedRoleId
属性:
Since you have a view model, I recommend you simplify your bindings and let your behind code handle the other functionalities. You can first make a SelectedRoleId
property:
public int SelectedRoleId { get; set; }
在您的视图模型中,可以将此属性的默认值分配给 User .UserRoles.ElementAt(0).RoleId
在您的构造函数或您的 OnGet
中,取决于您的设置方式。
In your view model, you can assign this property's default value to User.UserRoles.ElementAt(0).RoleId
in either your constructor or your OnGet
, based on how you need it set up. This way the drop down is bound to a simple property.
对于绑定下拉菜单(HTML选择),. NET Core提供了 asp项
For binding dropdowns (HTML selects) .NET Core provides the asp-items
tag helper.
<select asp-for="SelectedRoleId" asp-items="Model.RoleNameSL"></select>
在您的 OnPostAsync
中,您可以使用 SelectedRoleId
访问所选值。
In your OnPostAsync
, you can use SelectedRoleId
to access the selected value.
这篇关于如何比较数据库和客户端.Net Core 2中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!