Click here to Skip to main content
15,889,695 members
Home / Discussions / Web Development
   

Web Development

 
QuestionIt's probably just me... Pin
#realJSOP18-Oct-17 2:39
mve#realJSOP18-Oct-17 2:39 
AnswerRe: It's probably just me... Pin
Nathan Minier23-Oct-17 1:25
professionalNathan Minier23-Oct-17 1:25 
GeneralRe: It's probably just me... Pin
#realJSOP23-Oct-17 4:45
mve#realJSOP23-Oct-17 4:45 
GeneralRe: It's probably just me... Pin
Nathan Minier23-Oct-17 6:13
professionalNathan Minier23-Oct-17 6:13 
QuestionRelated to Table in angular 4 material Pin
Munjal Pandya6-Oct-17 22:18
Munjal Pandya6-Oct-17 22:18 
SuggestionRe: Related to Table in angular 4 material Pin
Richard Deeming9-Oct-17 8:55
mveRichard Deeming9-Oct-17 8:55 
GeneralRe: Related to Table in angular 4 material Pin
Munjal Pandya9-Oct-17 17:25
Munjal Pandya9-Oct-17 17:25 
QuestionMVC Edit form with dynamic dropdown list Pin
desanti23-Sep-17 8:45
desanti23-Sep-17 8:45 
Hello !
I'm using MVC , C# and Entity Framework.
The object on my model are:
State-------- Id , Name
City ------- Id , Name , StateId
TheObject----Id,Name,StateId,CityId
I want to create an edit form for TheObject.
The Edit form has 2 dropdownlist State and City that are created dynamically , and the City list depend on selection made on State List.
The problem is that the dropdown list are filled correctly , but when the edit form is open these 2 dropdownlist are in empty state and does not have selected the real values for the object that is edited .
The partial code for Edit view is this :
<div class="form-group">
            @Html.LabelFor(u => u.State, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(u => u.State,
              new SelectList(ViewBag.State, "Id", "Name"),
              "Choose State",
              new { @class = "form-control", @onchange = "selectCities()" })
                @Html.ValidationMessageFor(u => u.State, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(u => u.City, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(u => u.City,
       new SelectList(Enumerable.Empty<SelectListItem>(), "Id", "Name"),
              "Choose City",
              new { @class = "form-control" })
                @Html.ValidationMessageFor(u => u.City, "", new { @class = "text-danger" })
            </div>
        </div>

<script>
    function selectCities() {
        debugger;
        var stateId = $("#State").val();
        $.ajax({
            url: '/Home/selectCities',
            type: 'POST',
            datatype: 'application/json',
            contentType: 'application/json',
            data: JSON.stringify({ stateId: +stateId }),
            success: function (result) {
                $("#City").html("");
                $("#City").append
                ($('<option></option>').val(null).html("---choose City---"));
                $.each($.parseJSON(result), function (i, cty)
                { $("#City").append($('<option></option>').val(cty.Id).html(cty.Name)) })

            },
            error: function () { alert("Error !") },
        });
    }
    </script>


The partial code of the controller is this :
private void Fill_StateDropDownList()
       {
           var st = from d in db.States
                     orderby d.Name
                     select d;
           ViewBag.State = st.ToList();
       }

     [HttpPost]
       public ActionResult selectCities(string stId)
       {

          List < City > lstcity = new List < City > ();
           int stateiD = Convert.ToInt32(stId);
           lstgrupet = (from d in db.Citys
                     where d.StateID==stateiD
                    select d).ToList();
          string result= JsonConvert.SerializeObject(lstgrupet, Formatting.Indented,
          new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore});
          return Json(result, JsonRequestBehavior.AllowGet);
       }

 public ActionResult Edit(int? id)
       {
           if (id == null)
           {
               return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
           }
         TheObject obj = db.TheObjects.Find(id);
           if (user == null)
           {
               return HttpNotFound();
           }
           Fill_StateDropDownList()
           return View(obj);
       }

       [HttpPost, ActionName("Edit")]
       [ValidateAntiForgeryToken]
       public ActionResult EditPost(int? id)
       {
           if (id == null)
           {
               return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
           }
           var theobjectToUpdate = db.TheObjects.Find(id);

           if (TryUpdateModel(theobjectToUpdate, "",
              new string[] { "Name","StateId","CityId" }))
           {
                   try
                   {
                       db.SaveChanges();
                       return RedirectToAction("Index");
                   }
                   catch (Exception)
                   {
                       ModelState.AddModelError("", "Error.");
                   }

           }
           Fill_StateDropDownList()
           return View(theobjectToUpdate);
       }


modified 23-Sep-17 15:02pm.

AnswerRe: MVC Edit form with dynamic dropdown list Pin
Richard Deeming25-Sep-17 2:26
mveRichard Deeming25-Sep-17 2:26 
QuestionMVC entity framework web page - How to store logged user in a global variable Pin
desanti22-Sep-17 4:12
desanti22-Sep-17 4:12 
AnswerRe: MVC entity framework web page - How to store logged user in a global variable Pin
Richard Deeming22-Sep-17 4:51
mveRichard Deeming22-Sep-17 4:51 
GeneralRe: MVC entity framework web page - How to store logged user in a global variable Pin
desanti22-Sep-17 5:55
desanti22-Sep-17 5:55 
GeneralRe: MVC entity framework web page - How to store logged user in a global variable Pin
Richard Deeming22-Sep-17 6:05
mveRichard Deeming22-Sep-17 6:05 
QuestionDigital Signatures Pin
Nathan Minier19-Sep-17 7:08
professionalNathan Minier19-Sep-17 7:08 
QuestionLogin form php,mysql,ajax Pin
Dalee1716-Sep-17 14:55
Dalee1716-Sep-17 14:55 
AnswerRe: Login form php,mysql,ajax Pin
W Balboos, GHB11-Oct-17 5:46
W Balboos, GHB11-Oct-17 5:46 
Questionhtml warning Pin
dcof30-Aug-17 12:10
dcof30-Aug-17 12:10 
AnswerRe: html warning Pin
W Balboos, GHB20-Sep-17 2:18
W Balboos, GHB20-Sep-17 2:18 
AnswerRe: html warning Pin
Member 1349103928-Oct-17 12:56
Member 1349103928-Oct-17 12:56 
QuestionScroll Bar in Popup Window Pin
shahbaz shaikh27-Aug-17 3:37
shahbaz shaikh27-Aug-17 3:37 
QuestionHow do I configure dropzone.js? Pin
Member 1307448726-Aug-17 13:39
Member 1307448726-Aug-17 13:39 
Rant[REPOST]: How do I configure dropzone.js? Pin
Richard Deeming29-Aug-17 2:43
mveRichard Deeming29-Aug-17 2:43 
SuggestionCrystal Reports XI v. IIS 10 Pin
Zimriel11-Aug-17 6:04
Zimriel11-Aug-17 6:04 
GeneralRe: Crystal Reports XI v. IIS 10 Pin
Nathan Minier14-Aug-17 2:03
professionalNathan Minier14-Aug-17 2:03 
QuestionHow do you code an application modal? Pin
charlieg4-Aug-17 12:08
charlieg4-Aug-17 12:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.