- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试将复杂类型绑定(bind)到属性以编辑属性值并将更改保存在内存中,现在用于测试。
I get an exception when I execute Post.
这是我的编辑 Razor View 。
<h1>Edit:</h1>
<form method="post">
<label asp-for="EditClient.Name"> </label>
<input asp-for="EditClient.Name"
class="form-control"
readonly="readonly" />
<h3 class="text text-info">Pull Configuration</h3>
<div>
<label asp-for="EditClient.Pull.Protocol"> </label>
<input asp-for="EditClient.Pull.Protocol" class="form-control" />
<span asp-validation-for="EditClient.Pull.Protocol" class="text-danger"></span>
<label asp-for="EditClient.Pull.Host"> </label>
<input asp-for="EditClient.Pull.Host" class="form-control" />
<span asp-validation-for="EditClient.Pull.Host" class="text-danger"></span>
<label asp-for="EditClient.Pull.User"> </label>
<input asp-for="EditClient.Pull.User" class="form-control" />
<span asp-validation-for="EditClient.Pull.User" class="text-danger"></span>
<label asp-for="EditClient.Pull.Password"> </label>
<input asp-for="EditClient.Pull.Password" class="form-control" />
<span asp-validation-for="EditClient.Pull.Password" class="text-danger"></span>
<label asp-for="EditClient.Pull.PrivateKeyPath"> </label>
<input asp-for="EditClient.Pull.PrivateKeyPath" class="form-control" />
<span asp-validation-for="EditClient.Pull.PrivateKeyPath" class="text-danger"></span>
<label asp-for="EditClient.Pull.Port"> </label>
<input asp-for="EditClient.Pull.Port" class="form-control" />
<span asp-validation-for="EditClient.Pull.Port" class="text-danger"></span>
<label asp-for="EditClient.Pull.RemoteDirectory"> </label>
<input asp-for="EditClient.Pull.RemoteDirectory" class="form-control" />
<span asp-validation-for="EditClient.Pull.RemoteDirectory" class="text-danger"></span>
<label asp-for="EditClient.Pull.IsDecrypt"> </label>
<input asp-for="EditClient.Pull.IsDecrypt" class="form-control" />
<span asp-validation-for="EditClient.Pull.IsDecrypt" class="text-danger"></span>
</div>
<h3 class="text text-info">Push Configuration</h3>
<label asp-for="EditClient.Push.Protocol"> </label>
<input asp-for="EditClient.Push.Protocol" class="form-control" />
<span asp-validation-for="EditClient.Push.Protocol" class="text-danger"></span>
<label asp-for="EditClient.Push.Host"> </label>
<input asp-for="EditClient.Push.Host" class="form-control" />
<span asp-validation-for="EditClient.Push.Host" class="text-danger"></span>
<label asp-for="EditClient.Push.User"> </label>
<input asp-for="EditClient.Push.User" class="form-control" />
<span asp-validation-for="EditClient.Pull.User" class="text-danger"></span>
<label asp-for="EditClient.Push.Password"> </label>
<input asp-for="EditClient.Push.Password" class="form-control" />
<span asp-validation-for="EditClient.Pull.Password" class="text-danger"></span>
<label asp-for="EditClient.Push.PrivateKeyPath"> </label>
<input asp-for="EditClient.Push.PrivateKeyPath" class="form-control" />
<span asp-validation-for="EditClient.Pull.PrivateKeyPath" class="text-danger"></span>
<label asp-for="EditClient.Push.Port"> </label>
<input asp-for="EditClient.Push.Port" class="form-control" />
<span asp-validation-for="EditClient.Pull.Port" class="text-danger"></span>
<label asp-for="EditClient.Push.RemoteDirectory"> </label>
<input asp-for="EditClient.Push.RemoteDirectory" class="form-control" />
<span asp-validation-for="EditClient.Pull.RemoteDirectory" class="text-danger"></span>
<label asp-for="EditClient.Push.IsZip"> </label>
<input asp-for="EditClient.Push.IsZip" class="form-control" />
<span asp-validation-for="EditClient.Push.IsZip" class="text-danger"></span>
<label asp-for="EditClient.Push.IsEncrypt"> </label>
<input asp-for="EditClient.Push.IsEncrypt" class="form-control" />
<span asp-validation-for="EditClient.Push.IsEncrypt" class="text-danger"></span>
<button type="submit">
Submit
</button>
</form>
This is my EditModel
``````````````````````````````````````````````````
public class EditModel : PageModel
{
private readonly IClientConfiguration _config;
[BindProperty(SupportsGet = false)]
public ClientConfig EditClient { get; set; }
public EditModel(IClientConfiguration config)
{
this._config = config;
}
public IActionResult OnPost()
{
var _editedClient =
new ClientConfig(this.EditClient.Name, this.EditClient.Email, this.EditClient.Push, this.EditClient.Pull);
if(this._config.GetByName(_editedClient.Name) == null)
{
this._config.Add(_editedClient);
}
else
{
this._config.Edit(_editedClient);
}
return this.RedirectToPage("/DropBox/Detail", new { name = _editedClient.Name });
}
public IActionResult OnGet(string name)
{
this.EditClient = this._config.GetByName(name);
return this.Page();
}
}
This is my type
```````````````````````````````````````````````````````````
[JsonObject]
public class ClientConfig
{
[JsonProperty]
public string Name { get; }
[JsonProperty]
public IEnumerable<string> Email { get; }
[JsonProperty]
public PushConfig Push { get; }
[JsonProperty]
public PullConfig Pull { get; }
[JsonConstructor]
public ClientConfig(string name, IEnumerable<string> email, PushConfig push, PullConfig pull)
{
this.Name = name;
this.Email = email;
this.Push = push;
this.Pull = pull;
}
}
This is my IClientConfig Interface and the class who implements it
`````````````````````````````````````````````````````````````````````
public class InMemoryClientConfig : IClientConfiguration
{
public List<ClientConfig> GetAll()
{
return this.LoadClientsConfig().Clients
.ToList();
}
public ClientConfig Add(ClientConfig clientConfig)
{
var allClients = this.GetAll();
var exist = allClients.Contains(clientConfig);
if(!exist)
{
this.GetAll().Add(clientConfig);
return clientConfig;
}
return clientConfig;
}
public ClientConfig Edit(ClientConfig clientConfig)
{
var _edit = this.GetByName(clientConfig.Name);
if (_edit != null)
this.GetAll().Remove(_edit);
this.GetAll()
.Add(new ClientConfig(clientConfig.Name, clientConfig.Email, clientConfig.Push, clientConfig.Pull));
return clientConfig;
}
public ClientConfig Delete(int id)
{
throw new NotImplementedException();
}
Config LoadClientsConfig()
{
var deserialize = File.ReadAllText(@"C:\...\Path\ToJsonFile");
return JsonConvert.DeserializeObject<Config>(deserialize);
}
public ClientConfig GetByName(string name)
=> this.GetAll
().SingleOrDefault(x => x.Name == name);
}
问题子源于ClientConfig EditClient Property位于 EditModel: PageModel 类行#18。
get 工作正常但是当我尝试发布在表单页面上所做的更改时我得到 InvalidOperationException Could not create an instance of type 'ProjectName.Core.ClientConfig模型绑定(bind)的复杂类型不能是抽象类型或值类型,并且必须具有无参数构造函数。或者,为“EditClient”参数提供一个非空默认值。
我已经坚持了一天左右,并查看了其他堆栈问题及其答案,但我不理解所提供的答案和术语或示例。
抱歉,如果我没有说清楚我的问题,但这是我力所能及的。
如有任何帮助,我们将不胜感激。
再次感谢。
最佳答案
终于明白了。
执行以下操作,1. 确保您的标签名称与您要绑定(bind)的属性匹配。"
为您要绑定(bind)的类型重载一个空构造函数。
将 razor View 中的绑定(bind)模型属性作为参数传递给 Post 方法,并且不要忘记使用标记助手从 razor View 中传递它。
模型必须有一个Get;放;对于 Prop ,你正在使用。
绑定(bind)模型属性,添加属性[BindProperty(Support Get= False)]并确保你得到了;放;在上面。
如果您需要这方面的代码示例,请告诉我。
我希望这对某人有所帮助。哇!
关于c# - InvalidOperationException Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.CreateModel(ModelBindingContext bindingContext),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58017354/
我最近开始在 ASP.Net MVC 3 应用程序中使用 Signal R 库。我可以使用 Signal R 向客户端发送消息。但我注意到,如果我从另一个浏览器登录该应用程序,我会收到以下错误 - 异
我必须编写一个异步方法来与 Web 服务联系。这是我在 WebServiceHelper 类中的方法: public static Task SignIn(string username, stri
在 WPF 应用程序中,我有一个 BackgroundWorker 线程创建一个对象。我们称对象为 foo。 后台 worker 代码: SomeClass foo = new SomeClass()
Closed. This question needs to be more focused。它当前不接受答案。 想改善这个问题吗?更新问题,使其仅关注editing this post的一个问题。
This question already has answers here: InvalidOperationException after removing an element in an ar
我正在运行一个 .NET Core 应用程序,该应用程序正在监听 Azure 服务总线主题。当我运行该应用程序时,出现此错误: A sessionful message receiver cannot
我有一个 WPF 应用程序,其中 MainNavigationWindow 在其构造函数中注册了其他一些类的事件: SomeClass obj = new SomeClass(); obj.SomeE
我有一个 WPF 应用程序,其中 MainNavigationWindow 在其构造函数中注册了其他一些类的事件: SomeClass obj = new SomeClass(); obj.SomeE
编译程序时我没有收到任何错误,但是一旦我点击保存按钮我收到“InvalidOperationException 未处理”。 cmd.ExecuteNonQuery(); 然后突出显示,我在这上面花了一
我遇到了一个我完全不知道的问题。我收到以下错误消息: The specified Visual and the Visual do not share a common ancestor, so th
The contract type HelloIndigo.Service is not attributed with ServiceContractAttribute. In order to d
当我尝试返回要下载的文件时遇到了一些奇怪的问题,所以这是我的代码 string filePath = Path.Combine(Path1, Path2, filename); return File
我制作了一个程序,我想在其中手动更新数据 GridView 。- 我有一种方法可以通过清除 DGV 然后重新插入数据来刷新它。-使用设计器,我为 DGV 的 CellEndEdit 制作了一个事件处理
情况示意图 我开发了一个系统,可以将许多 Material (代码Matetiaal)添加到广告(代码Zoeker)。这种关系是多对多的。在这里你得到了我的类的结构。 +---------------
我有以下代码来构建从 SQL Server 中提取的高级数据结构,然后当该数据的检索完成时,我更新 UI。使用的代码是 private void BuildSelectedTreeViewSectio
我正在尝试使用表达式树,因为根据描述,这似乎是最正确(性能最好、可配置)的方法。 我希望能够编写一个语句,从 existingItems 集合中获取与 incomingItem 的 propertyN
问题 我有一个 MVVM 应用程序,它使用 Caliburn.Micro 作为 MVVM 框架,并使用 MEF 进行“依赖注入(inject)”(在引号中我知道它不是严格意义上的 DI 容器)。根据
我正在 try catch 声明变量时有时会发生的 InvalidOperationException。但是,以下代码不起作用。可能是因为我真的不知道你是如何捕捉到异常的。 public overri
在我尝试了很多很多解决方案之后,我无法以任何方式解决这个问题,所以我开始相信这个问题没有解决方案。 我有一个包含复杂属性的对象。例如:List .我正在工作线程上运行此类中的一个方法,以保持 GUI
情况: 我依靠默认的关联处理程序为各种文件类型(图片、Word 文档等)生成进程。这意味着我只将特定文件名指定为 StartInfo.FileName,并且在该文件名之前没有实际的可执行文件。同时我指
我是一名优秀的程序员,十分优秀!