0

私はMVC5プロジェクトでTwitter Bootstrap Typeaheadを使用しており、関連するレコードを正しく一覧表示しています。私はupdaterセクションで選択したレコードのId値を取得することはできますが、フォーム送信時に投稿することはできません。 Idパラメータのさまざまな組み合わせを試しましたが、意味がありませんでした。 Twitter Bootstrap TypeaheadでIdパラメータを投稿するには?TwitterでポストできないBootstrap Typeahead

ビュー:

@Html.HiddenFor(m => m.StudentId) 

<input id="StudentId" name="StudentId" type="text" class="form-control tt-input" 
    autocomplete="off" spellcheck="false" dir="auto"> 

<script> 
    $("#StudentId").typeahead({ 
     minLength: 1, 
     source: function (query, process) { 
      var lookups = []; 
      map = {}; 

      // This is going to make an HTTP post request to the controller 
      return $.post('/Grade/StudentLookup?query=%QUERY', { query: query }, function (data) { 

       // Loop through and push to the array 
       $.each(data, function (i, lookup) { 
        map[lookup.Name] = lookup; 
        lookups.push(lookup.Name); 
       }); 

       // Process the details 
       process(lookups); 
      }); 
     }, 
     updater: function (item) { 
      var selectedId = map[item].Id; 
      console.log("Selected : " + selectedId); 
      return item; 
     } 
    }); 
</script> 


コントローラ:名前とIDに

public ActionResult StudentLookup(string query) 
{ 
    var students = repository.Students.Select(m => new StudentViewModel 
    { 
     Id = m.Id, 
     Name = m.Name + " " + m.Surname     
    }) 
    .Where(m => m.Name.StartsWith(query)); 
    return Json(students, JsonRequestBehavior.AllowGet); 
} 
+0

コンソールエラーとは何でしょうか?console.logのマップと項目は、アップデータ関数とソース関数のルックアップで確認できますか?あなたの問題は 'map'はグローバルなものではないと思います。 – MarZab

+0

私はselectedIndexChangeでIdパラメータを "selectedId"として取得しますが、それをStudentIdモデル値に割り当てることはできません。ですから、唯一の問題はこれです。Model.StudentIdにどのように割り当てることができますか? –

+0

ああ、隠しフィールド/別のフィールドを使用してそのIDを割り当てることをお勧めします。そうしないと、名前が予想された場所に数字が配置された理由が混乱します。 – MarZab

答えて

1

洗面所独立フィールドは、あなたも、IDフィールドが非表示または読み取り専用にすることができます。

<input id="StudentName" type="text" class="form-control tt-input" 
    autocomplete="off" spellcheck="false" dir="auto"> 

<input id="StudentId" name="StudentId" type="text"> 

<script> 
    $("#StudentName").typeahead({ 
     minLength: 1, 
     source: function (query, process) { 
      var lookups = []; 
      map = {}; 

      // This is going to make an HTTP post request to the controller 
      return $.post('/Grade/StudentLookup?query=%QUERY', { query: query }, function (data) { 

       // Loop through and push to the array 
       $.each(data, function (i, lookup) { 
        map[lookup.Name] = lookup; 
        lookups.push(lookup.Name); 
       }); 

       // Process the details 
       process(lookups); 
      }); 
     }, 
     updater: function (item) { 
      var selectedId = map[item].Id; 
      console.log("Selected : " + selectedId); 
      $("#StudentId").val(selectedId) 
      return item; 
     } 
    }); 
</script> 
+0

はい、それは働いた。私はちょうど$( '#StudentId')で隠れた値を割り当てて間違っていました。代わりに** $( "#StudentId").val(selectedId); ** :)ありがとうございました..一方、ID値を転記するための先読みのスマートな機能はありますか? –

+0

落ち着いて、私は答えるのに1分必要です:Pいいえ、これはそれを行う方法です。私はいつもBloodhound https://twitter.github.io/typeahead.js/examples/#bloodhoundを使ってより良いレイアウトを実現しています。 – MarZab

+0

申し訳ありません:) TypeheadのBloodhoundのメリットについて教えてください。レイアウトだけですか? ASP.NET MVCについては、どちらをお勧めですか? –

関連する問題