发布后的MVC空列表

时间:2017-10-12 15:56:54

标签: asp.net-mvc

我的post方法是在数据发布后得到null模型对象(参数)。 我检查了所有文本框的名称,它们是:name =“Indeksy [0] .min” 我试图在这里搜索很多主题,但没有人帮助过我。 也许你们有任何想法,因为我看不到任何错误:/ 提前谢谢......

查看:

    @model AktualnyProjekt.Models.Glowna
<div id="widelki" hidden="hidden">
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        for (var k = 0; k < Model.Indeksy.Count; k++)
        {
            @Html.HiddenFor(i => i.Indeksy[k].IndeksId)

            <div class="form-group">
                @Html.LabelFor(model => model.Indeksy[k].nazwa_rodzaju, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.Indeksy[k].nazwa_rodzaju, new { @class = "form-control" })
                    @*@Html.TextBoxFor(model => item.nazwa_rodzaju, new { @class = "form-control", @readonly = "true" })*@
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Indeksy[k].poziom, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.Indeksy[k].poziom, new { @class = "form-control" })
                    @*@Html.TextBoxFor(model => item.nazwa_rodzaju, new { @class = "form-control", @readonly = "true" })*@
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Indeksy[k].min, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.Indeksy[k].min, new { @class = "form-control" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Indeksy[k].max, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.Indeksy[k].max, new { @class = "form-control" })
                </div>
            </div>
        }
    }
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()

        <div class="form-actions no-color">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    }
</div>

型号:

 public class Glowna
{
    //public IEnumerable<Log> Logs { get; set; }
    public IList<Indeks> Indeksy { get; set; }
}

控制器:

   [Authorize]
    [HttpGet]
    public ActionResult Index()
    {
          Glowna model = new Glowna();
          model.Indeksy=db.Indeksy;
          return (model);
    }


[Authorize]
[HttpPost]
public ActionResult Index(Glowna model)
{
    if(ModelState.IsValid)
    {
        if (model != null)
        {
            foreach (Indeks item in model.Indeksy)
            {
                Indeks inde = db.Indeksy.First(x => x.IndeksId == item.IndeksId);
                inde.min = item.min;
                inde.max = item.max;
            }
            db.SaveChanges();
        }
    }

    return RedirectToAction("Index","Home");
}

1 个答案:

答案 0 :(得分:0)

问题是您有两个表单,提交按钮的格式与数据不同。所以基本上你要发送空数据,因为在你的第二种形式中你只有按钮。

请删除您的第二个表单部分,然后将提交按钮移至第一个表单部分。

所以改变这个

@using (Html.BeginForm())
{
//data
}
@using (Html.BeginForm())
{
//submit button
}

由此

@using (Html.BeginForm())
{
 //data
 //submit button
}
  @model AktualnyProjekt.Models.Glowna
<div id="widelki" hidden="hidden">
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        for (var k = 0; k < Model.Indeksy.Count; k++)
        {
            @Html.HiddenFor(i => i.Indeksy[k].IndeksId)

            <div class="form-group">
                @Html.LabelFor(model => model.Indeksy[k].nazwa_rodzaju, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.Indeksy[k].nazwa_rodzaju, new { @class = "form-control" })
                    @*@Html.TextBoxFor(model => item.nazwa_rodzaju, new { @class = "form-control", @readonly = "true" })*@
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Indeksy[k].poziom, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.Indeksy[k].poziom, new { @class = "form-control" })
                    @*@Html.TextBoxFor(model => item.nazwa_rodzaju, new { @class = "form-control", @readonly = "true" })*@
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Indeksy[k].min, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.Indeksy[k].min, new { @class = "form-control" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Indeksy[k].max, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.Indeksy[k].max, new { @class = "form-control" })
                </div>
            </div>
        }

        @Html.AntiForgeryToken()

        <div class="form-actions no-color">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    }
</div>