InvalidOperationException Microsoft.AspNetCore.Mvc.ModelBind



我正在尝试将复杂类型绑定到属性以编辑属性值并将更改保存在内存中,现在用于测试。

I get an exception when I execute Post. 

这是我的编辑剃须刀视图。

<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:...PathToJsonFile");
return JsonConvert.DeserializeObject<Config>(deserialize);
}
public ClientConfig GetByName(string name)
=> this.GetAll
().SingleOrDefault(x => x.Name == name);
}



问题子项源于 ClientConfig EditClient 属性 位于编辑模型:页面模型类行 #18 中。

get工作正常,但是当我尝试发布在表单页面上所做的更改时,我得到无效操作异常无法创建类型为"项目名称.核心.客户端配置"的实例 模型绑定的复杂类型不能是抽象类型或值类型,并且必须具有无参数构造函数。或者,为"编辑客户端"参数提供一个非空默认值。

我已经在这个上面停留了一天左右,并查看了其他带有答案的堆栈问题,但我不明白提供的答案,术语 或示例。

抱歉,如果我没有说清楚我的问题,但这是我尽我所能。

任何协助将不胜感激。

再次感谢。

终于想通了。

执行以下操作, 1. 确保标签名称与要绑定到的属性匹配。 ">

  1. 重载要绑定到的类型的空构造函数。

  2. 将绑定模型属性作为参数从 razor 视图传递给 Post 方法,不要忘记使用标记帮助程序从 razor 视图传递它。

  3. 模型必须具有 Get;设置;对于道具,您正在使用。

  4. 绑定
  5. 模型属性,添加属性 [绑定属性(支持 Get = False(] 并确保你有一个得到;设置;在上面。

如果您需要为此的代码示例,请告诉我。

我希望这对某人有所帮助。 沃拉!

相关内容

  • 没有找到相关文章

最新更新