【问题标题】:ViewModel always empty in Post action [HttpPost]ViewModel 在 Post 操作中始终为空 [HttpPost]
【发布时间】:2021-05-25 08:49:12
【问题描述】:

我向 Web 服务发送一个请求,我得到的响应是一个名称和值的列表。我在视图中循环它们并呈现一个表单,以允许用户填写字段并提交它。但是 viewmodel 在 post action 中总是返回 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);
    }
}

有什么建议吗?

【问题讨论】:

  • 您需要提供更多信息。例如,您期望到达您的操作方法的模型是什么样的?
  • @DavidG 我已经更新了问题

标签: c# .net asp.net-mvc .net-core soap


【解决方案1】:

用 for 替换循环 foreach 并向表单添加操作

<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>
    

【讨论】:

    【解决方案2】:

    您必须在输入中添加“名称”属性:

      <input name="FlowOutputModel[@Model.FlowOutputModel.IndexOf(item)].CharacteristicValue" type="text" class="form-control" placeholder="@item.CharacteristicName *" value="" required />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-29
      • 1970-01-01
      • 2021-05-21
      • 1970-01-01
      • 1970-01-01
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多