英文:
View doesn't send model in post-method
问题
我是新手学习ASP.NET,并尝试为我的项目创建一个Web UI。用户应能够编辑另一个应用程序的配置文件。因此,他可以从列表中选择配置行,并获得一个新表单来编辑实际的行。
如果我使用POST
方法提交结果,则参数包含默认值。
视图:
@model Config
@{
ViewData["Title"] = "Configuration";
}
<div class="card">
<div class="card-header">
<h5>Edit Config</h5>
</div>
<form asp-action="EditConfig" method="post">
<div class="card-body">
<div class="row">
<div class="col form-group">
<label asp-for="Name">@Model.Name</label>
</div>
</div>
<div class="row">
<div class="col form-group">
<input asp-for="Value" class="form-control" />
</div>
</div>
<input type="hidden" value="Type" id="Type" />
</div>
<div class="card-footer">
<button type="submit" class="btn btn-primary">Speichern</button>
</div>
</form>
</div>
控制器中的POST
方法:
[HttpPost]
public IActionResult EditConfig(Config config)
{}
模型类Config
:
public class Config
{
public string Name { get; set; }
public object Value { get; set; }
public Type Type { get; set; }
public Config(string name, object value, Type type)
{
Name = name;
Value = value;
Type = type;
}
public Config() : this("", "", typeof(Type)) { }
}
我尝试使用隐藏输入,但似乎没有任何影响,因为甚至Value
属性也没有接收到值。
这里Type
属性搞乱了事情吗?
更新
我能够在David的评论和RG Orobia的答案的帮助下使它部分正常工作。问题似乎是属性的类型。
现在视图的代码是:
@model Config
@{
ViewData["Title"] = "Configuration";
}
<div class="card">
<div class="card-header">
<h5>Edit Config</h5>
</div>
<form asp-action="EditConfig" method="post">
<div class="card-body">
<div class="row">
<div class="col form-group">
<label asp-for="Name">@Model.Name</label>
<input hidden asp-for="Name" class="form-control" />
</div>
</div>
<div class="row">
<div class="col form-group">
<input asp-for="Value" class="form-control" />
</div>
</div>
<div class="row">
<div class="col form-group">
<input hidden asp-for="Type" class="form-control" />
</div>
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-primary">Speichern</button>
</div>
</form>
</div>
然而,Type
属性仍然没有获得正确的值,对我来说并不是什么问题,但我想我还是要在这里列出它。
英文:
I am new to ASP.NET and trying to create a Web UI for a project of mine.
The user should be able to edit a config file of the other application. For that reason he can select a config line from a list and gets a new form to edit the actual line.
If I then use the POST
method to submit the result the parameter contains the default values.
The view:
@model Config
@{
ViewData["Title"] = "Configuration";
}
<div class="card">
<div class="card-header">
<h5>Edit Config</h5>
</div>
<form asp-action="EditConfig" method="post">
<div class="card-body">
<div class="row">
<div class="col form-group">
<label asp-for="Name">@Model.Name</label>
</div>
</div>
<div class="row">
<div class="col form-group">
<input asp-for="Value" class="form-control" />
</div>
</div>
<input type="hidden" value="Type" id="Type" />
</div>
<div class="card-footer">
<button type="submit" class="btn btn-primary">Speichern</button>
</div>
</form>
</div>
The POST
method in the controller:
[HttpPost]
public IActionResult EditConfig(Config config)
{}
The model class Config
:
public class Config
{
public string Name { get; set; }
public object Value { get; set; }
public Type Type { get; set; }
public Config(string name, object value, Type type)
{
Name = name;
Value = value;
Type = type;
}
public Config() : this("", "", typeof(Type)) { }
}
I tried to use hidden inputs but that doesn't seem to make a difference since even the Value
property doesn't receive a value.
Is the Type
property messing things up here?
UPDATE
I was able to get it somewhat working thanks to David´s comment and RG Orobia´s answer.
The problem seams to be the types of the properties.
The code for the view is now:
@model Config
@{
ViewData["Title"] = "Configuration";
}
<div class="card">
<div class="card-header">
<h5>Edit Config</h5>
</div>
<form asp-action="EditConfig" method="post">
<div class="card-body">
<div class="row">
<div class="col form-group">
<label asp-for="Name">@Model.Name</label>
<input hidden asp-for="Name" class="form-control" />
</div>
</div>
<div class="row">
<div class="col form-group">
<input asp-for="Value" class="form-control" />
</div>
</div>
<div class="row">
<div class="col form-group">
<input hidden asp-for="Type" class="form-control" />
</div>
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-primary">Speichern</button>
</div>
</form>
</div>
However the Type
property still doesn't get the correct value, which isn't really a problem for me but i thought i still list it here.
答案1
得分: 2
似乎无法序列化 <code>Value</code>。一种解决方法是将 <code>Value</code> 更改为类型 <code>string</code>,然后在后端将该值转换为所需类型。
英文:
It seems like <code>Value</code> cannot be serialized. One workaround would be to change <code>Value</code> to type <code>string</code> and then cast the value to whatever type you want on the backend.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论