Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC4 Sending data from a text box in Controller to View using ActionLink and TempData
    primarykey
    data
    text
    <p>Is there a way to utilize TempData with a ActionLink to pass data from a View to a Controller. I have a View that has client-side validation and I need to update a drop down list from a text box on the page without using a submit button. When using two submit type buttons the client side validation flags all the fields that have not been filled in. Essentially I want to be able to add a user defined category to the drop down list without having to jump to a different view for just a textbox and submit button. </p> <p>I was not sure if I could do something like this:</p> <pre><code>&lt;input type="text" name="NewCat" value="@TempData["newCat"]" placeholder="New Catagory"/&gt; </code></pre> <p>I couldn't quite get it to work though...</p> <p><strong>The View:</strong></p> <pre><code>@model Exercise4.Models.Quote @{ ViewBag.Title = "Create"; } &lt;h2&gt;Create New Quote&lt;/h2&gt; @using (Html.BeginForm()) { @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;Quotation Creation&lt;/legend&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.CatagoryID, "Catagory") &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.DropDownList("CatagoryID", String.Empty) @Html.ValidationMessageFor(model =&gt; model.CatagoryID) &lt;/div&gt; &lt;aside&gt; @using (Html.BeginForm("NewCatagory","Quote")) { &lt;input type="text" name="NewCat" value="@TempData["newCat"]" placeholder="New Catagory"/&gt; @Html.ActionLink("Add Catagory","NewCatagory","Quote") } &lt;/aside&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.QName) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.TextBoxFor(model =&gt; model.QName) @Html.ValidationMessageFor(model =&gt; model.QName) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.QAuthor) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.QAuthor) @Html.ValidationMessageFor(model =&gt; model.QAuthor) &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; &lt;/fieldset&gt; } &lt;div&gt; @Html.ActionLink("Back to List", "Index") &lt;/div&gt; @section Scripts { @Scripts.Render("~/bundles/jqueryval") } </code></pre> <p><strong>The Controller Section:</strong></p> <pre><code>// // 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"); } </code></pre> <p>Any suggestions would be greatly appreciated either how to make this work or something a little more elegant.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload