Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# IList装订页_C#_Asp.net Core_Razor_Razor Pages - Fatal编程技术网

C# IList装订页

C# IList装订页,c#,asp.net-core,razor,razor-pages,C#,Asp.net Core,Razor,Razor Pages,正在尝试绑定IList,以便可以更新现有的字段。但是,结果是OnPostAsync的列表为空。不知道我错过了什么 我的代码如下所示: [BindProperty] public IList<Runbook_Serverlist_Plus> RunbookServerListPlus { get; set; } <form method="post"> <table class="table"> <thead&

正在尝试绑定IList,以便可以更新现有的字段。但是,结果是OnPostAsync的列表为空。不知道我错过了什么

我的代码如下所示:

[BindProperty]
public IList<Runbook_Serverlist_Plus> RunbookServerListPlus { get; set; }
<form method="post">
<table class="table">
  <thead>
  <tr>
    <th>
      @Html.DisplayNameFor(model => model.RunbookServerListPlus[0].Bc.Servername)
    </th>
    <th>
      @Html.DisplayNameFor(model => model.RunbookServerListPlus[0].Bc.Status)
    </th>
    <th>
      @Html.DisplayNameFor(model => model.RunbookServerListPlus[0].Bc.Cluster)
    </th>
    <th>
      @Html.DisplayNameFor(model => model.RunbookServerListPlus[0].Bc.StatusChange)
    </th>
    <th>
      @Html.DisplayNameFor(model => model.RunbookServerListPlus[0].Bc.PreRun)
    </th>
    <th>
      @Html.DisplayNameFor(model => model.RunbookServerListPlus[0].Bc.Ordering)
    </th>
    <th></th>
  </tr>
  </thead>
  <tbody>
  @for (var i = 0; i < Model.RunbookServerListPlus.Count; i++)
  {
    var item = Model.RunbookServerListPlus[i];
    <tr>
      <td>
        @Html.DisplayFor(modelItem => item.Bc.Servername)
      </td>
      <td>
        @Html.DisplayFor(modelItem => item.Bc.Status)
      </td>
      <td>
        @Html.DisplayFor(modelItem => item.Bc.Cluster)
      </td>
      <td>
        @Html.DisplayFor(modelItem => item.Bc.StatusChange)
      </td>
      <td>
        @Html.DisplayFor(modelItem => item.Bc.PreRun)
      </td>
      <td>
        @Html.DisplayFor(modelItem => item.Bc.Ordering)
      </td>
      <td>
        <input hidden asp-for="@item.Bc.Servername" class="form-control" />
        <input asp-for="@item.CheckCompleted"  class="form-control"/>
      </td>
    </tr>
  }
  </tbody>
</table>
  <div class="form-group">
    <input type="submit" value="Update" class="btn btn-primary" />
  </div>
</form>
因此,从我的模型中,我首先得到IList,然后为Runbook\u Serverlist\u Plus创建一个新列表。 想法是有一个Runbook\u Serverlist的基类,然后在上面放一个Runbook\u Serverlist\u Plus,我可以在其中设置特定的字段,以后可以使用这些字段更新某些字段

见下面的代码:

  public class Runbook_Serverlist_Plus
  {
    [BindProperty]
    public Runbook_Serverlist Bc {get; set; }

    public Runbook_Serverlist_Plus(Runbook_Serverlist bsBc)
    {
      Bc = bsBc;
      CheckCompleted = false;
    }
    
    public bool CheckCompleted { get; set; }
  }
在我的cshmtl中,代码如下所示:

[BindProperty]
public IList<Runbook_Serverlist_Plus> RunbookServerListPlus { get; set; }
<form method="post">
<table class="table">
  <thead>
  <tr>
    <th>
      @Html.DisplayNameFor(model => model.RunbookServerListPlus[0].Bc.Servername)
    </th>
    <th>
      @Html.DisplayNameFor(model => model.RunbookServerListPlus[0].Bc.Status)
    </th>
    <th>
      @Html.DisplayNameFor(model => model.RunbookServerListPlus[0].Bc.Cluster)
    </th>
    <th>
      @Html.DisplayNameFor(model => model.RunbookServerListPlus[0].Bc.StatusChange)
    </th>
    <th>
      @Html.DisplayNameFor(model => model.RunbookServerListPlus[0].Bc.PreRun)
    </th>
    <th>
      @Html.DisplayNameFor(model => model.RunbookServerListPlus[0].Bc.Ordering)
    </th>
    <th></th>
  </tr>
  </thead>
  <tbody>
  @for (var i = 0; i < Model.RunbookServerListPlus.Count; i++)
  {
    var item = Model.RunbookServerListPlus[i];
    <tr>
      <td>
        @Html.DisplayFor(modelItem => item.Bc.Servername)
      </td>
      <td>
        @Html.DisplayFor(modelItem => item.Bc.Status)
      </td>
      <td>
        @Html.DisplayFor(modelItem => item.Bc.Cluster)
      </td>
      <td>
        @Html.DisplayFor(modelItem => item.Bc.StatusChange)
      </td>
      <td>
        @Html.DisplayFor(modelItem => item.Bc.PreRun)
      </td>
      <td>
        @Html.DisplayFor(modelItem => item.Bc.Ordering)
      </td>
      <td>
        <input hidden asp-for="@item.Bc.Servername" class="form-control" />
        <input asp-for="@item.CheckCompleted"  class="form-control"/>
      </td>
    </tr>
  }
  </tbody>
</table>
  <div class="form-group">
    <input type="submit" value="Update" class="btn btn-primary" />
  </div>
</form>

@DisplayNameFor(model=>model.RunbookServerListPlus[0].Bc.Servername)
@DisplayNameFor(model=>model.RunbookServerListPlus[0].Bc.Status)
@DisplayNameFor(model=>model.RunbookServerListPlus[0].Bc.Cluster)
@DisplayNameFor(model=>model.RunbookServerListPlus[0].Bc.StatusChange)
@DisplayNameFor(model=>model.RunbookServerListPlus[0].Bc.PreRun)
@DisplayNameFor(model=>model.RunbookServerListPlus[0].Bc.Ordering)
@对于(var i=0;iitem.Bc.Servername)
@DisplayFor(modeleItem=>item.Bc.Status)
@DisplayFor(modeleItem=>item.Bc.Cluster)
@DisplayFor(modelItem=>item.Bc.StatusChange)
@DisplayFor(modeleItem=>item.Bc.PreRun)
@DisplayFor(modeleItem=>item.Bc.Ordering)
}
因此,我有一个复选框为CheckCompleted项目,当做一篇文章,我想看到所有的复选框标记。 当我提交/发布时,我希望RunbookServerListPlus被填充。然而,这个一直是空的

我错过了什么

Post功能目前没有其他功能:

      public async Task<IActionResult> OnPostAsync()
      {
        if (!ModelState.IsValid)
        {
          return Page();
        }

        return RedirectToPage("./Index");
      }
公共异步任务OnPostAsync() { 如果(!ModelState.IsValid) { 返回页(); } 返回页首(“/索引”); }
根据用于发出请求的HTTP谓词,每个请求只能触发一个处理程序<当使用HTTP Get方法请求页面时,执行code>OnGet
OnPost
由POST方法执行

网络是无状态的。这意味着在一个请求上初始化的变量对另一个请求不可用。如果要在
OnPost
方法中使用集合,则需要在
OnPost
处理程序中再次实例化它


有关我的站点的更多详细信息:

首先,您可以添加隐藏的输入来绑定模型数据,form post将传递输入的值。和带有name属性的.net core绑定模型。如果您想用list绑定数据,需要将name设置为
list[index].xxx

如果您只想在选中CheckCompleted时传递Runbook_Serverlist_Plus,那么可以在表单发布之前使用js将隐藏输入的名称更改为正确的格式

型号:

public class Runbook_Serverlist_Plus
    {
        [BindProperty]
        public Runbook_Serverlist Bc { get; set; }
        public Runbook_Serverlist_Plus(Runbook_Serverlist bsBc)
        {
            Bc = bsBc;
            CheckCompleted = false;
        }
        public Runbook_Serverlist_Plus()
        {
        }
        public bool CheckCompleted { get; set; }
    }
    public class Runbook_Serverlist
    {
        public string Servername { get; set; }
        public string Status { get; set; }
        public string Cluster { get; set; }
        public string StatusChange { get; set; }
        public string PreRun { get; set; }
        public string Ordering { get; set; }


    }
cshtml:

<form method="post">
    <table class="table">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.RunbookServerListPlus[0].Bc.Servername)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.RunbookServerListPlus[0].Bc.Status)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.RunbookServerListPlus[0].Bc.Cluster)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.RunbookServerListPlus[0].Bc.StatusChange)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.RunbookServerListPlus[0].Bc.PreRun)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.RunbookServerListPlus[0].Bc.Ordering)
                </th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @for (var i = 0; i < Model.RunbookServerListPlus.Count; i++)
            {
                var item = Model.RunbookServerListPlus[i];
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.Bc.Servername)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Bc.Status)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Bc.Cluster)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Bc.StatusChange)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Bc.PreRun)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Bc.Ordering)
                    </td>
                    <td>
                        <input asp-for="@item.CheckCompleted" class="form-control" name="RunbookServerListPlus[index].CheckCompleted" />
                        <input hidden asp-for="@item.Bc.Servername" class="form-control" name="RunbookServerListPlus[index].Bc.Servername" />
                        <input hidden asp-for="@item.Bc.Status" class="form-control" name="RunbookServerListPlus[index].Bc.Status" />
                        <input hidden asp-for="@item.Bc.Cluster" class="form-control" name="RunbookServerListPlus[index].Bc.Cluster" />
                        <input hidden asp-for="@item.Bc.StatusChange" class="form-control" name="RunbookServerListPlus[index].Bc.StatusChange" />
                        <input hidden asp-for="@item.Bc.PreRun" class="form-control" name="RunbookServerListPlus[index].Bc.PreRun" />
                        <input hidden asp-for="@item.Bc.Ordering" class="form-control" name="RunbookServerListPlus[index].Bc.Ordering" />

                    </td>
                </tr>
            }
        </tbody>
    </table>
    <div class="form-group">
        <input type="submit" value="Update" class="btn btn-primary" />
    </div>
</form>
<script>
    $("form").submit(function () {
        var index = 0;
        $("tbody tr").each(function () {
            var lasttd = $(this).find('td:last-child');
            
            if (lasttd.find("input")[0].checked) {
                lasttd.find("input").each(function () {
                    $(this).attr("name", $(this).attr("name").replace("index",index));
                })
                index++;
            }
        })
    })
</script>

@DisplayNameFor(model=>model.RunbookServerListPlus[0].Bc.Servername)
@DisplayNameFor(model=>model.RunbookServerListPlus[0].Bc.Status)
@DisplayNameFor(model=>model.RunbookServerListPlus[0].Bc.Cluster)
@DisplayNameFor(model=>model.RunbookServerListPlus[0].Bc.StatusChange)
@DisplayNameFor(model=>model.RunbookServerListPlus[0].Bc.PreRun)
@DisplayNameFor(model=>model.RunbookServerListPlus[0].Bc.Ordering)
@对于(var i=0;iitem.Bc.Servername)
@DisplayFor(modeleItem=>item.Bc.Status)
@DisplayFor(modeleItem=>item.Bc.Cluster)
@DisplayFor(modelItem=>item.Bc.StatusChange)
@DisplayFor(modeleItem=>item.Bc.PreRun)
@DisplayFor(modeleItem=>item.Bc.Ordering)
}
$(“表格”)。提交(函数(){
var指数=0;
$(“tbody tr”)。每个(函数(){
var lasttd=$(this.find('td:last child');
if(lasttd.find(“输入”)[0]。选中){
lasttd.find(“输入”).each(函数(){
$(this.attr(“name”),$(this.attr(“name”).replace(“index”,index));
})
索引++;
}
})
})
cs(我使用假数据进行测试):

[BindProperty]
公共列表RunbookServerListPlus{get;set;}
公共互联网
{
RunbookServerListPlus=新列表{
新Runbook_Serverlist_Plus{Bc=新Runbook_Serverlist{Cluster=“c1”,Ordering=“1”
<td>
                    <input asp-for="@item.CheckCompleted" class="form-control" name="RunbookServerListPlus[@i].CheckCompleted" />
                    <input hidden asp-for="@item.Bc.Servername" class="form-control" name="RunbookServerListPlus[@i].Bc.Servername" />
                    <input hidden asp-for="@item.Bc.Status" class="form-control" name="RunbookServerListPlus[@i].Bc.Status" />
                    <input hidden asp-for="@item.Bc.Cluster" class="form-control" name="RunbookServerListPlus[@i].Bc.Cluster" />
                    <input hidden asp-for="@item.Bc.StatusChange" class="form-control" name="RunbookServerListPlus[@i].Bc.StatusChange" />
                    <input hidden asp-for="@item.Bc.PreRun" class="form-control" name="RunbookServerListPlus[@i].Bc.PreRun" />
                    <input hidden asp-for="@item.Bc.Ordering" class="form-control" name="RunbookServerListPlus[@i].Bc.Ordering" />

                </td>