【问题标题】:Submitting a model in a View back to the controller comes back as NULL将视图中的模型提交回控制器返回为 NULL
【发布时间】:2020-12-08 00:07:01
【问题描述】:

0

我有一个模型,其中包含一个列表作为一个属性成员。当我使用字符串列表执行 HTTP GET 时,它运行良好,因为我构建了一个文本框列表,其中包含列表中的值,但是当我使用 HTTP POST 执行“提交”时,所有内容都会正确传回,但是List 字段返回为 NULL。为什么传回的是 NULL 而不是字符串列表?

从模型中提取:

public class OIRModel
{
    public string SearchCadenceName { get; set; }
    public string SearchVendor { get; set; }
    public List<string> _Vendors { get; set; }
}

从视图中提取:

@foreach (var item in Model._Vendors)
{
        <tr>
            <td>@Html.TextBoxFor(x => item)</td>
        </tr>
}

从控制器中提取:

public ActionResult EditPost(OIRModel _model)

【问题讨论】:

  • &lt;form /&gt; 标签的 HTML 是什么样的?

标签: asp.net asp.net-core model-view-controller .net-core


【解决方案1】:

您可以构建如下文本框列表:

@for (int i = 0; i < Model._Vendors.Count(); i++)
{
    <tr>
        <td>@Html.TextBoxFor(x => Model._Vendors[i])</td>
    </tr>
}

【讨论】:

  • 嗨@Mark C,如果不是这样,您可以向我们展示整个表单代码。
猜你喜欢
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 2020-04-29
  • 2016-04-24
  • 2013-01-08
相关资源
最近更新 更多