TempData を ActionLink で利用して、View から Controller にデータを渡す方法はありますか。クライアント側の検証を行うビューがあり、送信ボタンを使用せずにページのテキスト ボックスからドロップダウン リストを更新する必要があります。2 つの送信タイプ ボタンを使用すると、クライアント側の検証によって、入力されていないすべてのフィールドにフラグが立てられます。基本的には、テキスト ボックスだけのために別のビューにジャンプすることなく、ユーザー定義のカテゴリをドロップダウン リストに追加できるようにしたいと考えています。そして送信ボタン。
次のようなことができるかどうかはわかりませんでした:
<input type="text" name="NewCat" value="@TempData["newCat"]" placeholder="New Catagory"/>
私はそれをうまく機能させることができませんでした...
景色:
@model Exercise4.Models.Quote
@{
ViewBag.Title = "Create";
}
<h2>Create New Quote</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Quotation Creation</legend>
<div class="editor-label">
@Html.LabelFor(model => model.CatagoryID, "Catagory")
</div>
<div class="editor-field">
@Html.DropDownList("CatagoryID", String.Empty)
@Html.ValidationMessageFor(model => model.CatagoryID)
</div>
<aside>
@using (Html.BeginForm("NewCatagory","Quote"))
{
<input type="text" name="NewCat" value="@TempData["newCat"]" placeholder="New Catagory"/>
@Html.ActionLink("Add Catagory","NewCatagory","Quote")
}
</aside>
<div class="editor-label">
@Html.LabelFor(model => model.QName)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.QName)
@Html.ValidationMessageFor(model => model.QName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.QAuthor)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.QAuthor)
@Html.ValidationMessageFor(model => model.QAuthor)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
コントローラ セクション:
//
// Create New Catagory
public ActionResult NewCatagory()
{
string value = TempData["newCat"] as string;
if(!String.IsNullOrWhiteSpace(value))
{
Catagory cat = new Catagory();
cat.CatName = value;
db.Catagories.Add(cat);
db.SaveChanges();
}
ViewBag.CatagoryID = new SelectList(db.Catagories, "CatagoryID", "CatName");
return RedirectToAction("Create");
}
これを機能させる方法またはもう少しエレガントなものを作成する方法についての提案は大歓迎です。