Post 操作 [HttpPost] 中的 ViewModel 始终为空
ViewModel always empty in Post action [HttpPost]
我向网络服务发送了一个请求,我得到的响应是一个名称和值的列表。我在视图中循环它们并呈现一个表单,以允许用户填写字段并提交。但是视图模型在 post 动作中总是 returns null。
这是我的看法:
<form method="post">
<div class="row register-form">
@foreach (var item in Model.FlowOutputModel)
{
<div class="col-md-6">
<div class="form-group">
<label for="@item.CharacteristicValue">
@item.CharacteristicName:
</label>
<input asp-for="@item.CharacteristicValue" type="text"
class="form-control"
placeholder="@item.CharacteristicName *" value="" required/>
</div>
</div>
}
<div class="col-md-12">
<input type="submit" class="btnRegister" value="Submit"/>
</div>
</div>
</form>
我的模型如下:
public class DecisionInputModel
{
public string FlowName { get; set; }
public List<FlowOutputModel> FlowOutputModel { get; set; }
}
public class FlowOutputModel
{
public string CharacteristicName { get; set; }
public string CharacteristicValue { get; set; }
}
我的控制器
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Index(DecisionInputModel decisionInput)
{
if (ModelState.IsValid)
{
_service.GetResults(decisionInput);
}
}
有什么建议吗?
您必须将“名称”属性添加到输入中:
<input name="FlowOutputModel[@Model.FlowOutputModel.IndexOf(item)].CharacteristicValue" type="text" class="form-control" placeholder="@item.CharacteristicName *" value="" required />
将循环 foreach 替换为 for 并向表单添加操作
<form method="post" action="@Url.Action("Index", "Home")">
<input asp-for="@Model.FlowName" type="text" class="form-control" />
// or maybe even better
@Html.TextBoxFor(model => model.FlowName, new { @class = "form-control" })
<div class="row register-form">
@for(int i=0; i < Model.FlowOutputModel.Count; i++)
{
<div class="col-md-6">
<div class="form-group">
<label for="@Model.FlowOutputModel[i].CharacteristicValue">
@Model.FlowOutputModel[i].CharacteristicName:
</label>
<input asp-for="@Model.FlowOutputModel[i].CharacteristicValue" type="text"
class="form-control"
placeholder="@Model.FlowOutputModel[i].CharacteristicName *" value="" required/>
</div>
</div>
}
<div class="col-md-12">
<input type="submit" class="btnRegister" value="Submit"/>
</div>
</div>
</form>
我向网络服务发送了一个请求,我得到的响应是一个名称和值的列表。我在视图中循环它们并呈现一个表单,以允许用户填写字段并提交。但是视图模型在 post 动作中总是 returns null。
这是我的看法:
<form method="post">
<div class="row register-form">
@foreach (var item in Model.FlowOutputModel)
{
<div class="col-md-6">
<div class="form-group">
<label for="@item.CharacteristicValue">
@item.CharacteristicName:
</label>
<input asp-for="@item.CharacteristicValue" type="text"
class="form-control"
placeholder="@item.CharacteristicName *" value="" required/>
</div>
</div>
}
<div class="col-md-12">
<input type="submit" class="btnRegister" value="Submit"/>
</div>
</div>
</form>
我的模型如下:
public class DecisionInputModel
{
public string FlowName { get; set; }
public List<FlowOutputModel> FlowOutputModel { get; set; }
}
public class FlowOutputModel
{
public string CharacteristicName { get; set; }
public string CharacteristicValue { get; set; }
}
我的控制器
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Index(DecisionInputModel decisionInput)
{
if (ModelState.IsValid)
{
_service.GetResults(decisionInput);
}
}
有什么建议吗?
您必须将“名称”属性添加到输入中:
<input name="FlowOutputModel[@Model.FlowOutputModel.IndexOf(item)].CharacteristicValue" type="text" class="form-control" placeholder="@item.CharacteristicName *" value="" required />
将循环 foreach 替换为 for 并向表单添加操作
<form method="post" action="@Url.Action("Index", "Home")">
<input asp-for="@Model.FlowName" type="text" class="form-control" />
// or maybe even better
@Html.TextBoxFor(model => model.FlowName, new { @class = "form-control" })
<div class="row register-form">
@for(int i=0; i < Model.FlowOutputModel.Count; i++)
{
<div class="col-md-6">
<div class="form-group">
<label for="@Model.FlowOutputModel[i].CharacteristicValue">
@Model.FlowOutputModel[i].CharacteristicName:
</label>
<input asp-for="@Model.FlowOutputModel[i].CharacteristicValue" type="text"
class="form-control"
placeholder="@Model.FlowOutputModel[i].CharacteristicName *" value="" required/>
</div>
</div>
}
<div class="col-md-12">
<input type="submit" class="btnRegister" value="Submit"/>
</div>
</div>
</form>