2017-10-12 11 views
0

マイポストメソッドはヌルモデルオブジェクト(パラメータ)データポスト後になっています。 テキストボックスのすべての名前を確認しました。名前= "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 } 

を変更するには、どのような形のデータが送信されているクロムインスペクタのXHRで確認しました。モデルのGlownaにもリストプロパティが1つしかないようです。私があなただったら私は最初にpublic ActionResult Index(** FormCollection ** form)メソッドを試してから先に進んで –

答えて

0

問題は2つのフォームがあり、サブミットボタンがあなたのデータと異なるフォームにあります。したがって、基本的にnullデータを送信しています。なぜなら、2番目のフォームにはボタンしかないからです。

したがって、2番目のフォームセクションを削除し、ボタンを最初のフォームセクションに移動します。

だから、この

@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

関連する問題