2017-10-12 64 views
0

我的post方法在數據發佈後獲取null模型對象(參數)。 我檢查了所有文本框的名稱,它們是這樣的:name =「Indeksy [0] .min」 我試圖在這裏搜索很多主題,但沒有人幫助過我。 也許你們有什麼想法,因爲我看到can'y任何錯誤:/ 在此先感謝...MVC空列表發佈後

查看:

@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"); 
} 
+0

改變這種

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

你在正在發送的表單數據的Chrome檢查XHR檢查。 Glowna模型似乎只有一個列表屬性。如果我是你,我會首先嚐試公共的ActionResult索引(** FormCollection **表格)方法,然後繼續前進 –

回答

0

問題是你有兩種形式,你的提交按鈕的形式與您的數據不同。所以基本上你是發送空數據,因爲在你的第二種形式中,你只有按鈕。

因此,刪除您的第二個表單部分,並將您的提交按鈕移到第一個表單部分。

因此,通過這種

@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> 
+0

我無法相信自己...你省了幾個我喜歡的houres。接受的答案,TY! – Maciek