2016-04-20 12 views
3

私はasp.netを使い慣れていませんが、簡単な編集メソッドを作るのはばかげています。私はたくさんのグーグル・グーグルをしましたが、それは助けにはなりませんでした。私はこの問題を認めていましたが、答えは私の場合には関連しませんでした。asp.net投稿の後のヌル?モデルを編集する

私はメソッドを作成して作られましたが、編集方法の間に、フォームからデータを取得した後、属性のすべてが空または0

のいずれかに縫い目私はこの上で二日間過ごし、持ってきました何をすべきかわからない。助けてください。

これらはコントローラの二つの方法

public IActionResult Edit(int? id) 
     { 
      if (id == null) 
      { 
       return HttpNotFound(); 
      } 

      Question question = _context.questions.Single(m => m.id == id); 
      if (question == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(question); 
     } 


     [HttpPost] 
     public IActionResult Edit(Question question) 
     { 
      if(question.id == 0) 
       return HttpNotFound(); 

      //  _context.questions.Attach(question); 
      //   _context.Entry(question).State = EntityState.Modified; 
      //   _context.SaveChangesAsync(); 

      return RedirectToAction("Index"); 

     } 

あるビューの形態

<!DOCTYPE html> 

<html> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>Edit</title> 
</head> 
<body> 

<form asp-action="Edit" method="post" > 
    <div class="form-horizontal"> 
     <h4>Question</h4> 
     <hr /> 
     <div asp-validation-summary="ValidationSummary.ModelOnly" class="text-danger"></div> 
     <div class="form-group"> 
      <input asp-for="id" class="form-control" /> 
     </div> 
     <div class="form-group"> 
      <label asp-for="question" class="col-md-2 control-label"></label> 
      <div class="col-md-10"> 
       <input asp-for="question" class="form-control" /> 
       <span asp-validation-for="question" class="text-danger" /> 
      </div> 
     </div> 
     <div class="form-group"> 
      <label asp-for="title" class="col-md-2 control-label"></label> 
      <div class="col-md-10"> 
       <input asp-for="title" class="form-control" /> 
       <span asp-validation-for="title" class="text-danger" /> 
      </div> 
     </div> 
     <div class="form-group"> 
      <label asp-for="answer" class="col-md-2 control-label"></label> 
      <div class="col-md-10"> 
       <input asp-for="answer" class="form-control" /> 
       <span asp-validation-for="answer" class="text-danger" /> 
      </div> 
     </div> 
     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Save" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
</form> 

<div> 
    <a asp-action="Index">Back to List</a> 
</div> 

@section Scripts { 
    <script src="~/lib/jquery/dist/jquery.min.js"></script> 
    <script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script> 
    <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script> 
} 
</body> 
</html> 

名前空間Daycare.Models {publicクラス質問 {

public Question() { 

    } 

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int id { get; set; } 
    public string title { get;set;} 

    public string question { get; set; } 

    public string answer { get; set; } 
    public DateTime date { get; set; } 
} 

}であります

モデルは、Int Idといくつかの文字列属性を持つ非常に単純なものです。

このエラーはなぜ発生していますか教えてください。

+1

質問モデルを表示できますか? – garethb

+1

@garethb質問:フォームだけでなく、あなたのモデルと全体のビューを表示してください。 – Stephen

+0

ブラウザのデベロッパーツールからネットワークを確認し、そのデータが送信されているかどうか、どのように送信されているかを確認してください。 – Spluf

答えて

0

バグのようでした。 ビューモデルを作成し、それを使用してモデルを助けました。

ごめんなさい遅く投稿

2

入力に名前がなく、Razorヘルパーを使用して作成していないため、HttpPost Editメソッドでモデルが空である理由があります。ここで

は、編集ビューの例です:

@model ControleDeProducaoWeb.Models.EquipamentosViewModel 

<h2>@ViewBag.Title</h2> 
<hr /> 
@using (Html.BeginForm("Save", ViewContext.RouteData.Values["controller"].ToString(), FormMethod.Post, new { @class = "form-horizontal" })) 
{ 
    <div class="row"> 
     <div class="col-sm-6"> 
      <div class="form-group"> 
       <label class="col-sm-3 control-label">Id</label> 
       <div class="col-sm-4"> 
        @Html.TextBoxFor(x => x.Id, new { @class = "form-control", @disabled = "disabled" }) 
       </div> 
      </div> 

      <div class="form-group"> 
       <label class="col-sm-3 control-label">Coligada</label> 
       <div class="col-sm-4"> 
        @Html.DropDownListFor(x => x.ColigadaId, ViewData.Model.Coligadas, new { @class = "form-control", @id = "lista-coligadas" }) 
       </div> 
      </div> 

      <div class="form-group"> 
       <label class="col-sm-3 control-label">Filial</label> 
       <div class="col-sm-4"> 
        @Html.DropDownListFor(x => x.FilialId, ViewData.Model.Filiais, new { @class = "form-control", @id = "lista-filiais" }) 
       </div> 
      </div> 

      <div class="form-group"> 
       <label class="col-sm-3 control-label">Cala Mínima</label> 
       <div class="col-sm-4"> 
        @Html.TextBoxFor(x => x.CalaMin, new { @class = "form-control" }) 
       </div> 
      </div> 

      <div class="form-group"> 
       <label class="col-sm-3 control-label">Cala Máxima</label> 
       <div class="col-sm-4"> 
        @Html.TextBoxFor(x => x.CalaMax, new { @class = "form-control" }) 
       </div>   
      </div> 
     </div> 
     <div class="col-sm-6"> 
      <div class="form-group"> 
       <label class="col-sm-3 control-label">Comprimento Máximo</label> 
       <div class="col-sm-4"> 
        @Html.TextBoxFor(x => x.ComprimentoMax, new { @class = "form-control" }) 
       </div> 
      </div> 

      <div class="form-group"> 
       <label class="col-sm-3 control-label">Altura Máxima</label> 
       <div class="col-sm-4"> 
        @Html.TextBoxFor(x => x.AlturaMax, new { @class = "form-control" }) 
       </div> 
      </div> 

      <div class="form-group"> 
       <label class="col-sm-3 control-label">Largura Máxima</label> 
       <div class="col-sm-4"> 
        @Html.TextBoxFor(x => x.LarguraMax, new { @class = "form-control" }) 
       </div> 
      </div> 

      <div class="form-group"> 
       <div class="col-sm-4 col-sm-offset-3"> 
        <a href="@Request.UrlReferrer.AbsoluteUri" class="btn btn-danger pull-right">Cancelar</a> 
        <button class="btn btn-success" type="submit">Salvar</button> 
       </div> 
      </div> 

     </div> 
    </div> 
} 
+0

私はあなたに答えていただきありがとうございます、私は家に帰ると数時間後にそれを試してみます! どのようにタグヘルパーでこれを達成できますか? 私が見たチュートリアル(多くのものがありました)のほとんどでお勧めしました。 – Greenmachine

+0

タグヘルパーは、MVC 6の新機能です。 '@addTagHelper" *、Microsoft.AspNet.Mvc.TagHelpers "を「GlobalImports.cshtml」に追加しましたか?また、コントローラをフォームのasp-controllerタグに追加します。 –

+0

いいえ、私はそれらをグローバルなインポートに追加していませんが、フォームは確実に正しいコントローラに向けられています。 – Greenmachine

関連する問題