Note that there are some explanatory texts on larger screens.

plurals
  1. POMvc4 having 2 forms in one view
    primarykey
    data
    text
    <p>So i'm building a simple mvc4 application, I have created all base models for the creation of the DB, from these classes I could naturally create basic controllers with their matching views.<br><br> Now my problem: I have the basic create actionresult + view and in this view I wanted that the user would be able to select some values from a dropdownlist which would make creation of new objects simpler.<br><br> I tried to achieve this with a second form in my view (the first one is the basic create form) now if I want to use these dropdowns (they filter each other(so first the user must specify a continent, then the dropdown of the countries only shows countries from that continent and after he specifies a country the region dropdown gets updated :) )) the submit of the basic view is always automatically called.</p> <p>so making the dropdowns update themselves isn't the problem :s it's that the form for the create automatically validates when the dropdowns are updated</p> <p>this is the controller where the dropdowns filter each other</p> <pre><code>// // GET: /FederationCenter/Create public ActionResult Create(string searchRegion, string searchCountry, string searchContinent) { var countries = from c in db.Countries select c; if (!String.IsNullOrEmpty(searchContinent)) { Continent searchContinentEnumValue = (Continent)Enum.Parse(typeof(Continent), searchContinent); countries = from c in db.Countries where ((int)c.Continent).Equals((int)searchContinentEnumValue) select c; } var regions = from r in db.Regions where r.Country.Name.Equals(searchCountry) select r; ViewBag.searchContinent = new SelectList(Enum.GetNames(typeof(SchoolCup.Domain.Continent))); ViewBag.searchCountry = new SelectList(countries, "Name", "Name"); ViewBag.searchRegion = new SelectList(regions, "Name", "Name"); return View(); } // // POST: /FederationCenter/Create [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(NSSF nssf, string searchRegion, string searchCountry, string searchContinent) { var countries = from c in db.Countries select c; if (!String.IsNullOrEmpty(searchContinent)) { Continent searchContinentEnumValue = (Continent)Enum.Parse(typeof(Continent), searchContinent); countries = from c in db.Countries where ((int)c.Continent).Equals((int)searchContinentEnumValue) select c; } var regions = from r in db.Regions where r.Country.Name.Equals(searchCountry) select r; ViewBag.searchContinent = new SelectList(Enum.GetNames(typeof(SchoolCup.Domain.Continent))); ViewBag.searchCountry = new SelectList(countries, "Name", "Name"); ViewBag.searchRegion = new SelectList(regions, "Name", "Name"); if (ModelState.IsValid) { var naam = Request["searchRegion"]; Region creatie = (from c in db.Regions where c.Name.Equals(naam) select c).SingleOrDefault(); nssf.ISFId = 1; nssf.RegionId = creatie.RegionId; db.NSSFs.Add(nssf); db.SaveChanges(); return RedirectToAction("Index"); } return View(nssf); } </code></pre> <p>and this is my view</p> <pre><code>@model SchoolCup.Domain.POCO.NSSF @{ ViewBag.Title = "Create"; } &lt;h2&gt;Create NSSF&lt;/h2&gt; &lt;div&gt; @using (Html.BeginForm(null, null, FormMethod.Post, new { name = "form" })) { @Html.AntiForgeryToken() @Html.DropDownList("searchContinent", null, "-- All continents --", new { onchange = "sendForm()" }) @Html.DropDownList("searchCountry", null, "-- All countries --", new { onchange = "sendForm()" }) @Html.DropDownList("searchRegion", null, "-- All regions --", new { onchange = "sendForm()" }) &lt;&gt; &lt;input type="submit" name= value="Search" /&gt; } &lt;/div&gt; @using (Html.BeginForm()) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;NSSF&lt;/legend&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Name) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Name) @Html.ValidationMessageFor(model =&gt; model.Name) &lt;/div&gt; </code></pre> <p>some more inputs</p> <pre><code> &lt;/fieldset&gt; &lt;p&gt; &lt;input type="submit" value="Create" /&gt; @Html.ActionLink("Back to List", "Index", null, new { @class = "button" }) &lt;/p&gt; } @section Scripts { &lt;script type="text/javascript"&gt; function sendForm() { document.form.submit() } &lt;/script&gt; } </code></pre> <p>I've been looking for at least a day and I don't know how to solve this</p> <p>with regards Alexander</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.
 

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