问题描述
我有一个用户控件,其中包含使用模型绑定的 ListView
。到现在为止还挺好。我想根据用户操作视图模式的方式列出对象。为此,我有一个名为Roles的公共财产。但是当我在那里调用 TryUpdateModel()
时,我收到了异常:
I have a user control that contains a ListView
that is using model binding. So far so good. I want to a list of objects based on how the user has manipulated the view mode. To this end I have a public property called Roles. However when I call TryUpdateModel()
in there I receive the exception:
TryUpdateModel' must be passed a value provider or alternatively must be invoked from inside a data-operation method of a control that uses model binding for data binding
现在我知道我可以通过以下方式退出编辑模式:
Now while I understand I can drop out of edit mode by doing:
lvData.EditIndex = -1;
然后在 UpdateMethod
中调用 TryUpdateModel()
我想知道如何调用TryUpdateModel而不必连接方法来进行更新。换句话说,我为IValueProvider参数提供的方式/位置/方式为 TryUpdateModel()
。
and then in the UpdateMethod
calling TryUpdateModel()
I was wondering how I could call TryUpdateModel without having to wire up the method to do the update. To put it another way how/where/what do I supply for the IValueProvider parameter to TryUpdateModel()
.
/// <summary>
/// Initialise the user control
/// </summary>
/// <param name="aRoles">List of roles to display</param>
public void Activate(List<RoleInfo> aRoles)
{
//List we will be binding
_ViewModel = new List<MembershipRolesViewModel>();
//Transfer the supplied list into the view model
foreach (RoleInfo roleInfo in aRoles)
{
_ViewModel.Add(new MembershipRolesViewModel
{
RoleDisplayName = roleInfo.RoleDisplayName,
RoleHint = roleInfo.RoleHint,
RoleName = roleInfo.RoleName,
RoleSelected = roleInfo.RoleSelected
});
}
}
//ListView.SelectMethod points here
public IQueryable<MembershipRolesViewModel> RolesSelect()
{
return _ViewModel.AsQueryable();
}
//Property to return the roles as manipulated by the user
public List<RoleInfo> Roles
{
get
{
List<RoleInfo> result = new List<RoleInfo>();
TryUpdateModel(_ViewModel);
foreach (MembershipRolesViewModel membershipRolesViewModel in _ViewModel)
{
result.Add(new RoleInfo
{
RoleDisplayName = membershipRolesViewModel.RoleDisplayName,
RoleHint = membershipRolesViewModel.RoleHint,
RoleName = membershipRolesViewModel.RoleName,
RoleSelected = membershipRolesViewModel.RoleSelected
});
}
return result;
}
}
TryUpdateModel
必须传递一个值提供者,或者必须从数据操作方法中调用一个使用模型绑定进行数据绑定的控件。
TryUpdateModel
must be passed a value provider or alternatively must be invoked from inside a data-operation method of a control that uses model binding for data binding.
推荐答案
这篇关于在数据操作方法之外调用TryUpdateModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!