OK-コントローラー/作成から、モデルの2つの部分を更新したい:
- ジャーナル
- 著者
Journal \ Create-基本的なジャーナルを作成し、著者のテーブルをクエリして、オートコンプリート入力ボックスに著者名を入力します(JQuery、tokenInputを使用)。
これは問題なく機能します。これまでのところ、ジャーナルの詳細を入力するための空白のフォームと、1人以上の著者を選択するための優れた方法があります。
私が困惑しているのは、署名が次のように、選択した作成者をコントローラーに戻す方法です。
[HttpPost]
public ActionResult Create(JOURNAL journal)
{
//persist the journal object in the database ....
but where to get the authors that were picked in the input box??? and persist those
in the model?
}
今、私は手を出して、私が使用できることを発見しました
Request.Form["authorlist"];
これでIDが返されますが(方法はよくわかりませんが??)、区切り文字がなくてもすべて結合されているため、あまり意味がありません。
1564654324544434344797361679
そのはず
1564,6543,2454,4434,3447,9736,1679
その後、データを使って何かを行うことができます。
とにかく、入力ボックスからより良い方法で結果を取得する方法と、モデルオブジェクトを使用してデータベースにデータを入力する方法を知っているなら、それは素晴らしいことです。
以下のコードは次のとおりです。
見る
@using (Html.BeginForm()){
@Html.ValidationSummary(true)
<fieldset>
<legend>JOURNAL</legend>
<div class="editor-label">
@Html.LabelFor(model => model.TITLE)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TITLE)
@Html.ValidationMessageFor(model => model.TITLE)
</div>
<div class="editor-label">
Select Authors</div>
<div class="authors">
<div class="editor-field">
<input type="text" id="authorlist" name="q"/>
</div>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
JAVASCRIPT
$("#authorlist").tokenInput('/author/getauthors/', {
hintText: "Enter surname please..",
searchingText: "Searching...",
preventDuplicates: true,
allowCustomEntry: true,
highlightDuplicates: false
});
CONTROLLER(作成者のリストを取得します)
public JsonResult GetAuthors(string term)
{
Debug.WriteLine("Term is: " + term);
term = term.ToUpper();
var authors = db.AUTHOR
.Where(a => a.FULL_NAME.ToUpper().StartsWith(term))
.Select(a => new { id = a.AUTHOR_ID, name = a.FULL_NAME });
return Json(authors, JsonRequestBehavior.AllowGet);
}