Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
58 views
in Technique[技术] by (71.8m points)

c# - better way to load 2 dropdown in mvc

This is how i am loading on page load state and city dropdown:

My Controller method:

This is the first method which is calling when page is loaded.

public ActionResult Index()
{
    var states = GetStates();
    var cities =  Enumerable.Empty<SelectListItem>();
    ViewBag.States = states;
    ViewBag.Cities = cities;
}

private IEnumerable<SelectListItem> GetStates()
{
    using (var db = new DataEntities())
    {
        return db.States.Select(d => new SelectListItem { Text = d.StateName, Value =d.Id.ToString() });
    }
}

[HttpGet]
public ActionResult GetCities(int id)
{
    using (var db = new DataEntities())
    {
        var data = db.Cities.Where(d=>d.StateId==id).Select(d => new { Text = d.CityName, Value = d.Id }).ToList();
        return Json(data, JsonRequestBehavior.AllowGet);
    }
}

My View:

IEnumerable<SelectListItem> States = ViewBag.States;
IEnumerable<SelectListItem> Cities = ViewBag.Cities;

@Html.DropDownList("State", States, "Select State", new { onchange="loadCities(this)"})
@Html.DropDownListFor(m => m.CityId, Cities, "Select City", new { id="ddlCity"})
function loadCities(obj) {
            $.ajax({
                url: "/Home/GetCities",
                data: { id: $(obj).val() },
                contentType:"application/json",
                success:function(responce){                   
                    var html = '<option value="0">Select City</option>';
                    $(responce).each(function () {
                        html += '<option value="'+this.Value+'">'+this.Text+'</option>'
                    });
                    $("#ddlCity").html(html);
                }
            });
        }

Any better way then this to load state and city dropdown?

public class HomeController : Controller
    {
        public ActionResult Index(int id=0)
        {
            Person model = null;
            var states = GetStates().ToList();
            var cities =  Enumerable.Empty<SelectListItem>();
            if (id > 0)
            {
                using (var db = new  DataEntities())
                {
                    model = db.People.Include("City").FirstOrDefault(d => d.Id == id);
                    if (model == null)
                        model = new Person();
                    else
                    {
                       states.First(d => d.Value == model.City.StateId.ToString()).Selected = true;
                       cities = db.Cities.Where(d => d.StateId == model.City.StateId).ToList().Select(d => new SelectListItem { Text = d.CityName,Value=d.Id.ToString(),Selected=d.Id==model.CityId });
                    }

                }
            }
            else
            {
                model = new Person();
            }
            ViewBag.States = states;
            ViewBag.Cities = cities;
            ViewBag.Persons = GetPersons();
            return View(model);
        }

        [HttpGet]
        public ActionResult GetCities(int id)
        {
            using (var db = new DataEntities())
            {
                var data = db.Cities.Where(d=>d.StateId==id).Select(d => new { Text = d.CityName, Value = d.Id }).ToList();
                return Json(data, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult SavePersonDetail([Bind(Exclude = "Id")] Person model)
        {

            // var employeeDal= new Emploee();
            //employee.firstname=model.

            if (ModelState.IsValid)
            {
                var Id = model.Id;
                int.TryParse(Request["Id"], out Id);
            using (var db = new DataEntities())
            {
                if (Id > 0)
                {
                    var person = db.People.FirstOrDefault(d => d.Id == Id);
                    if (person != null)
                    {
                        model.Id = Id;
                        db.People.ApplyCurrentValues(model);
                    }
                }
                else
                {
                    db.People.AddObject(model);                    
                }
                db.SaveChanges();
                }               
            }
            if (!Request.IsAjaxRequest())
            {
                ViewBag.States = GetStates();
                ViewBag.Persons = GetPersons();
                ViewBag.Cities = Enumerable.Empty<SelectListItem>();
                return View("Index");
            }
            else
            {
                return PartialView("_personDetail",GetPersons());
            }
        }

        public ActionResult Delete(int id)
        {
            using (var db = new DataEntities())
            {
                var model = db.People.FirstOrDefault(d => d.Id == id);
                if (model != null)
                {
                    db.People.DeleteObject(model);
                    db.SaveChanges();
                }                
            }
            if (Request.IsAjaxRequest())
            {
                return Content(id.ToString());
            }
            else
            {
                ViewBag.States = GetStates();
                ViewBag.Persons = GetPersons();
                ViewBag.Cities = Enumerable.Empty<SelectListItem>();
                return View("Index");
            }
        }

        private IEnumerable<SelectListItem> GetStates()
        {
            using (var db = new DataEntities())
            {
               return db.States.ToList().Select(d => new SelectListItem { Text = d.StateName, Value =d.Id.ToString() });
            }
        }

        private IEnumerable<Person> GetPersons()
        {
            using (var db = new DataEntities())
            {
                return db.People.Include("City").Include("City.State").ToList();
            }
        }

        public ActionResult HomeAjax()
        {
            ViewBag.States = GetStates();
            ViewBag.Cities = Enumerable.Empty<SelectListItem>();
            using (var db = new DataEntities())
            {
                var data = db.States.Include("Cities").Select(d => new { Id = d.Id, Name = d.StateName, Cities = d.Cities.Select(x => new { Id=x.Id,Name=x.CityName}) }).ToList();
                ViewBag.CityStateJson = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(data);
            }
            ViewBag.Persons = GetPersons();
            return View();
        }
    }


@model IEnumerable<Person>
<div>
    <table>
        <tr>
            <th>
                First Name
            </th>
            <th>
                Last Name
            </th>
            <th>
                Email
            </th>
            <th>
                City
            </th>
            <th>
                State
            </th>
            <th>
                Edit
            </th>
        </tr>
    @if (Model.Count() == 0)
    {
        <tr>
            <td colspan="6">
                <h3>No data available</h3>
            </td>
        </tr>
    }
    else { 
    foreach (var item in Model) { 
        <tr data-id="@item.Id">
            <td data-id="fn">@item.FirstName</td>
            <td data-id="ln">@item.LastName</td>
            <td data-id="email">@item.Email</td>
            <td data-id="cn">@item.CityName<input type="hidden" value="@item.CityId" /></td>
            <td>@item.StateName</td>
            <td>
                @if (ViewBag.Title == "Home Ajax" || Request.IsAjaxRequest())
                {
                    <a href="javascript:void(0);" onclick="Edit(this,@item.Id);">Update</a>
                    <span>@Ajax.ActionLink("Delete", "Delete", new { id = item.Id }, new AjaxOptions {OnSuccess="deleteSuccess",OnBegin="showLoader",OnComplete="hideLoader" })</span>

                }
                else { 
                    <span>@Html.ActionLink("Update", "Index", new { id = item.Id })</span>
                    <span>@Html.ActionLink("Delete", "Delete", new { id = item.Id })</span>
                }

            </td>
        </tr>
    }

    }
        </table>
</div>

@model Person

@{
    ViewBag.Title = "Home Ajax";
    IEnumerable<Person> persons = ViewBag.Persons;
    IEnumerable<SelectListItem> States = ViewBag.States;
    IEnumerable<SelectListItem> Cities = ViewBag.Cities;
    IEnumerable<State> fullStates=ViewBag.CityStates;

}

@section featured {
    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1>@ViewBag.Title.</h1>                
            </hgroup>            
        </div>
    </section>
}

@section styles{
    <style type="text/css">
       td,th {
            border:1px solid;
            padding:5px 10px;
        }

        select {
           padding:5px 2px;
           width:310px;
           font-size:16px;
        }
    </style>
}

@section scripts{
    @Scripts.Render("~/bundles/jqueryval")
    <script type="text/javascript">

        var jsonArray = @Html.Raw(ViewBag.CityStateJson)

        function clearValues() {
            $("input[type='text'],select").val('');
            $("input[type='hidden'][name='Id']").val(0);
        }

        function loadCities(obj) {

            for (var i = 0; i < jsonArray.length; i++) {
                if (jsonArray[i].Id == parseInt($(obj).val())) {
                    fillCity(jsonArray[i].Cities);
                    break;
                }
            }
        }

        function Edit(obj, Id) {
            //  alert("hi")
            $("input[type='hidden'][name='Id']").val(Id);
            var tr = $(obj).closest("tr");
            $("#txtfirstName").val($("td[data-id='fn']", tr).text().trim());
            $("#txtlastName").val($("td[data-id='ln']", tr).text().trim());
            $("#txtemail").val($("td[data-id='email']", tr).text().trim());
            var city = $("td[data-id='cn'] input[type='hidden']", tr).val();
            var state;
            for (var i = 0; i < jsonArray.length; i++) {
                for (var j = 0; j < jsonArray[i].Cities.length; j++) {
                    if (jsonArray[i].Cities[j].Id == parseInt(city)) {
                        state = jsonArray[i].Id;
                        break;
                    }
                }
                if (state) {
                    fillCity(jsonArray[i].Cities);
                    break;
                }
            }
            $("#ddlState").val(state);
            $("#ddlCity").val(city);
        }

        function fillCity(obj) {
            var html = '<option value="0">Select City</option>';
            $(obj).each(function () {
                html += '<option value="' + this.Id + '">' + this.Name + '</option>'
            });
            $("#ddlCity").html(html);
        }

        function deleteSuccess(responce) {
            alert("record deleted successfully");
            $("tr[data-id='" + responce + "']").remove();
        }

        function insertSuccess() {
            a

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You approach using ajax is fine although I would recommend a few better practices including using a view model with properties for StateID, CityID StateList and CityList, and using Unobtrusive JavaScript rather than polluting you markup with behavior, and generating the first ("please select") option with a null value rather than 0 so it can be used with the [Required] attribute

HTML

@Html.DropDownList(m => m.StateID, States, "Select State") // remove the onchange
@Html.DropDownListFor(m => m.CityID, Cities, "Select City") // why change the default ID?

SCRIPT

var url = '@Url.Action("GetCities", "Home")'; // use the helper (dont hard code)
var cities = $('#CityID'); // cache the element
$('#StateID').change(function() {
  $.getJSON(url, { id: $(this).val() }, function(response) {
    // clear and add default (null) option
    cities.empty().append($('<option></option>').val('').text('Please select'));
    $.each(response, function(index, item) {
      cities.append($('<option></option>').val(item.Value).text(item.Text));
    });
  });
});

If you were rendering multiple items (say you were asking the user to select their last 10 cities they visited), you can cache the result of the first call to avoid repeated calls where their selections may include cities from the same state.

var cache = {};
$('#StateID').change(function() {
  var selectedState = $(this).val();
  if (cache[selectedState]) {
    // render the options from the cache
  } else {
    $.getJSON(url, { id: selectedState }, function(response) {
      // add to cache
      cache[selectedState] = response;
      .....
    });
  }
});

Finally, in response to your comments regarding doing it without ajax, you can pass all the cities to the view and assign them to a javascript array. I would only recommend this if you have a few countries, each with a few cities. Its a matter of balancing the slight extra initial load time vs the slight delay in making the ajax call.

In the controller

model.CityList = db.Cities.Select(d => new { City = d.CountryID, Text = d.CityName, Value = d.Id }).ToList();

In the view (script)

// assign all cities to javascript array
var allCities= JSON.parse('@Html.Raw(Json.Encode(Model.CityList))');
$('#StateID').change(function() {
  var selectedState = $(this).val();
  var cities = $.grep(allCities, function(item, index) {
    return item.CountryID == selectedState;
  });
  // build options based on value of cities
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...