2017-03-10 3 views
6

私のビューにはEditorForがあります。データベースからテーブル内のすべてのNULLを見つけて、いくつかの値で更新コントローラでこのEditorForからメソッドに渡す値

また
@Html.EditorFor(model => model.First().Link, 
    new { htmlAttributes = new { @class = "form-control", placeholder = "Email", id= "start" } }) 

Iアクション、同様 は、ここで私が更新に行くボタンをタップインデックスビューでコード

public ActionResult Update(string start="lol") 
{ 
    ApplicationDbContext context = new ApplicationDbContext(); 

    IEnumerable<InvitationMails> customers = context.InvitationMails 
      .Where(c => c.Link == null) 
      .AsEnumerable() 
      .Select(c => { 
       c.Link = start; 
       return c; 
      }); 
    foreach (InvitationMails customer in customers) 
    { 
     // Set that row is changed 
     context.Entry(customer).State = EntityState.Modified; 
    } 
    context.SaveChanges(); 
    return RedirectToAction("Index"); 
} 

ですここ それを行動し、起動コード

<ul class="btn btn-default" style="width: 150px; list-style-type: none; font-size: 16px; margin-left: 20px"> 
       <li style="color: white">@Html.ActionLink("Добавить почту", "Update", "InvitationMails", null, new { @style = "color:white" })</li> 
      </ul> 

である。しかし、ここで静的な値で更新され、私はビューから値を受け取るしたいです。 コードをどのように書く必要がありますか?

+1

なぜあなたはコレクションであり、あなたのモデルにまったく関係のないものにバインドしようとしているモデルを持っていますか?あなたの達成しようとしていることは不明です。あなたのパラメータにバインドするには、入力に 'name =" start "が必要です。あなたは何にもバインドしていないので、入力を手動で作成してください。 –

+1

そして、あなたのメソッドにGETを行っているように見えるアクションリンクを表示しましたが、入力値は渡しません。 –

答えて

1

基本的には、nameレンダリングの属性をinputに設定する必要があります。

MVC 4を使用している場合、このケースでは、空間的過負荷はEditorForです。あなたはもうid="start"を必要としない

@Html.EditorFor(model => model.First().Link, 
    null, 
    "start", //That will set id and name attributes to start 
    new { @class = "form-control", placeholder = "Email" }) 

注:

あなたはこのようにそれを使用することができます。あなたの更新の後

あなたは基本的に2つのオプションがあります。

1Sオプション - formを使用します。

@using (Html.BeginForm("Update", "InvitationMails", FormMethod.Post)) //maby you need GET 
{ 
    @Html.EditorFor(model => model.First().Link, 
     null, 
     "start", //That will set id and name attributes to start 
     new { @class = "form-control", placeholder = "Email" }) 

     <ul class="btn btn-default" style="width: 150px; list-style-type: none; font-size: 16px; margin-left: 20px"> 
      <li style="color: white"> 
       <button type="submit" style="color:white">Добавить почту</button> 
      </li> 
     </ul> 
} 

第二オプション - アクションリンクをクリック上のJSを使用しています。

+0

コードを試してください。動作しません。私は今質問を更新するでしょう、私はそれを明確に書いていないかもしれません –

+0

@ E.Sコントローラに渡すあなたの 'start'値はどこにありますか? –

+0

私はEditorForからテキストを取り出し、Update Actionの結果に渡す必要があります –

関連する問題