c# - Code first, Data is not inserting into database using cascading dropdown list -
In my [HTPT post] method, excluding all the data cascading drop down items, I have joined the department, subject and Section models are: There may be several sections of a department, there may be several sections of a topic. After submitting Jquery Deposit, it shows, the form can not be submitted! So it comes back to the index! Here is my code from the section controller:
[http post] [Valid Antiforganic tokens] Make public anecscent (section section) {if (ModelState.IsValid) {db.Sections.Add (section) ; Db.SaveChanges (); Return Redirect Action ("Index"); } ViewBag.DepartmentId = New Selection List (DB Dept., "DepartmentDirectory", "Name", Section Department ID); ViewBag.SubjectId = New Selection List (DB Shabeks, "Subject ID", "Subject Name", Section, Shadam Idi); See Return (Section); } My creation under the Sieakon View. HTML is:
@model.models.Section @ {ViewBag.Title = "Create section"; } Action Script {& lt; Script type = "text / javascript" & gt; $ (Function () {$ .getJSON ("/ section / department / list", function (data) {var item = "& lt; option & gt; show list of department & lt; / option & gt;" $ Each (data, function i, department) {item + = "and lieutenant; option value = '" + department. Value + "'" gt; + + department. Text + "& lt; / option & gt;";} ); $ ("Value"), function (function () {$ .getJSON ("/ section / topic / list /" + $ ("# section> option: selected"). (Data) {var item = "& lt; Option> Show topic list & lt; / option & gt;"; $ .each (data, f) (Value,) {item = = "& lt; option value $ (" # subject "). Html (item);})}}}}} ="; $ (function () {$ ('' submit ("Click", function () {var form = $ (this) .Parent ("form"); $ .ajax ({type: "POST" Url: form.attr ('verb') , Data: form.serialize ()} .success (function () {warning ("your form has been submitted");}) .error (function () {alert ("Your form is not submitted"); }); Return false;});}); & lt; / script & gt;} & lt; h2 & gt; create section & lt; / H2 & gt; @using (Html.BeginForm ()) {@ Html.AntiForgeryToken () @ HTML.ValidationSummary (true) & lt; Fieldet & gt; & Lt; Story & gt; Section & lt; / Narrative & gt; & Lt; Label = "section" & gt; Departments & lt; / Label & gt; & Lt; Id = "section" name = "section" & gt; & Lt; / Select & gt; & Lt; Label = "topic" & gt; Subject & lt; / Label & gt; & Lt; Select ID = "topic" name = "topic" & gt; & Lt; / Select & gt; & Lt; Label = "section" & gt; Section & lt; / Label & gt; & Lt; Input id = "section" name = "section" type = "text" /> & Lt; P & gt; & Lt; Input type = "submit" id = "submit" value = "create" /> & Lt; / P & gt; & Lt; / Fieldset & gt; } & Lt; Div & gt; @html.exe link ("back to list", "index") & lt; / Div & gt;
I think you should have name / id your section Regarding your properties in both of your dropdown list. section to DepartmentId and subject to SubjectId . They need to match in their posts , the binder will attach the correct value
Comments
Post a Comment