Click here to Skip to main content
15,887,746 members
Home / Discussions / Web Development
   

Web Development

 
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 
AnswerRe: MVC Edit form with dynamic dropdown list Pin
Richard Deeming25-Sep-17 2:26
mveRichard Deeming25-Sep-17 2:26 
Controller:
C#
private void Fill_StateDropDownList()
{
    var st = from d in db.States
             orderby d.Name
             select d;
    
    ViewBag.State = st.ToList();
}

private void Fill_CityDropDownList(int? stateId)
{
    var cities = from d in db.Citys
                 where d.StateID == stateId
                 select d;
    
    ViewBag.City = cities.ToList();
}

[HttpPost]
public ActionResult selectCities(int? stateId)
{
    var cities = from d in db.Citys
                 where d.StateID == stateId
                 select new { d.Id, d.Name };
    
    return Json(cities, JsonRequestBehavior.AllowGet);
}

public ActionResult Edit(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    
    TheObject obj = db.TheObjects.Find(id);
    if (obj == null)
    {
        return HttpNotFound();
    }
    
    Fill_StateDropDownList();
    Fill_CityDropDownList(obj.StateId);
    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 ex)
        {
            ModelState.AddModelError("", ex);
        }
    }
    
    Fill_StateDropDownList();
    Fill_CityDropDownList(theobjectToUpdate.StateId);
    return View(theobjectToUpdate);
}

View:
<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" })
        @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(ViewBag.City, "Id", "Name"),
            "Choose City",
            new { @class = "form-control" })
        @Html.ValidationMessageFor(u => u.City, "", new { @class = "text-danger" })
    </div>
</div>

Javascript:
JavaScript
$(function(){
    $("#State").change(function(){
        debugger;
        var stateId = $(this).val();
        $.post('/Home/selectCities', { stateId: +stateId })
            .done(function(result){
                var $city = $("#City");
                $city.html("");
                $city.append($('<option/>').val(null).html("---choose City---"));
                $.each(result, function (i, cty) { 
                    $city.append($('<option/>').val(cty.Id).html(cty.Name));
                });
            })
            .fail(function(){
                alert("Error !");
            });
    });
});




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


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 
AnswerRe: How do you code an application modal? Pin
Kornfeld Eliyahu Peter5-Aug-17 10:02
professionalKornfeld Eliyahu Peter5-Aug-17 10:02 

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.