都道府県を選択できるようにする
ぐるなびWebサービスの都道府県マスタAPIを利用
図8-33改変
都道府県モデルクラスを追加 Models/Pref.vb
Public Class Pref
Public Property code As String
Public Property name As String
End Class
リスト8-28(以下太字を追加)
Function GourmetSearch() As ActionResult
Dim keyid = "0XXXXXXXXXXXXXX9" '無料登録により取得したアクセスキー
Dim doc = XElement.Load(String.Format("http://api.gnavi.co.jp/ver2/PrefSearchAPI/?keyid={0}", keyid))
Dim prefItem = doc.Elements.Select(Function(p) New Pref With {.code = p.Elements("pref_code").Value, .name = p.Elements("pref_name").Value})
ViewBag.Pref = prefItem.Select(Function(p) New SelectListItem With {.Value = p.code, .Text = p.name})
Return View()
End Function
Function GourmetResult(ByVal keyword As String, ByVal pref_code As String) As ActionResult
System.Threading.Thread.Sleep(3000)
If Request.IsAjaxRequest Then
Dim keyid = "0XXXXXXXXXXXXXX9"
Dim prefid = pref_code
・・・中略・・・
リスト8-30
・・・中略・・・
<h2>GourmetSearch</h2>
<form>
<label> 都道府県</label>
@Html.DropDownList("Pref", DirectCast(ViewBag.Pref, IEnumerable(Of SelectListItem)),
"選択してください",
New With {.class = "pref_list", .id = "pref_code"})
<br />
<label for="keyword">キーワード</label>
・・・中略・・・
$('#search').click(function () {
$('#result').load('/Ajax/GourmetResult', { keyword: $('#keyword').val() ,pref_code:$('#pref_code').val()});
});
・・・中略・・・