0% found this document useful (0 votes)
61 views7 pages

Drop Down List J Query

The code defines controllers to manage country, state, and city data in a database using ASP.NET MVC. The CountryController provides actions to display, create, and save country records. It includes a method to automatically generate unique IDs. Similarly, the StateController and CityController manage state and city data respectively, including dependent dropdown lists to select related country and state values during city creation. The Create.cshtml view renders a form to create new cities with validation.

Uploaded by

satish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views7 pages

Drop Down List J Query

The code defines controllers to manage country, state, and city data in a database using ASP.NET MVC. The CountryController provides actions to display, create, and save country records. It includes a method to automatically generate unique IDs. Similarly, the StateController and CityController manage state and city data respectively, including dependent dropdown lists to select related country and state values during city creation. The Create.cshtml view renders a form to create new cities with validation.

Uploaded by

satish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

CountryController.

cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace PAMS.Controllers
{
public class CountryController : Controller
{
PAMS2017Entities2 obj = new PAMS2017Entities2();

[HttpGet]
public ActionResult Index()
{
return View(obj.Countries.ToList());
}

private Country AutoGenerateId() //autogenerate


{
Country c = new Country();
var i = (from x in obj.Countries
orderby x.id descending
select x).Take(1); //return max(id)
int y = 0;
foreach (var item in i)
{
y = item.id;
}
y++;
c.Coid = "co_" + y;
return c;
}
[HttpGet]
public ActionResult Create()
{
Country c = AutoGenerateId();

return View(c);
}
[HttpPost]
public ActionResult Create(Country c1)
{
obj.Countries.Add(c1);
obj.SaveChanges();
return View();
}
}
}

Code for Statecontroller.cs:-


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace PAMS.Controllers
{
public class StateController : Controller
{

PAMS2017Entities2 obj = new PAMS2017Entities2();

public ActionResult Index()


{
return View(obj.States.ToList());
}

private State AutoGenerateId() //autogenerate


{
State s = new State();
var i = (from x in obj.States
orderby x.id descending
select x).Take(1); //return max(id)
int y = 0;
foreach (var item in i)
{
y = item.id;
}
y++;
s.Stateid = "st_" + y;
return s;
}
[HttpGet]
public ActionResult Create()
{
var country = obj.Countries.ToList();
State s = AutoGenerateId();
List<SelectListItem> li = new List<SelectListItem>();
li.Add(new SelectListItem { Text = "Select country", Value = "0" });
foreach (var item in country)
{
li.Add(new SelectListItem { Text = item.Coname, Value = item.Coid });
}
TempData["c"] = li;
TempData.Keep();
return View(s);
}
[HttpPost]
public ActionResult Create(State s1)
{

obj.States.Add(s1);
obj.SaveChanges();
return View();
}
} }
Code for CityController.cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PAMS.Controllers
{
public class CityController : Controller
{
PAMS2017Entities2 obj = new PAMS2017Entities2();
[HttpGet]
public ActionResult Index()
{
var city = obj.Cities.ToList();
return View(city);

public JsonResult getstate(string id)


{
var states = obj.States.Where(x => x.Coid == id).ToList();
List<SelectListItem> listates = new List<SelectListItem>();
listates.Add(new SelectListItem { Text = "--Select State--", Value = "0" });
if (states != null)
{
foreach (var x in states)
{
listates.Add(new SelectListItem { Text = x.StateName, Value = x.Stateid });
}
}
return Json(new SelectList(listates, "Value", "Text", JsonRequestBehavior.AllowGet));
}
private City AutoGenerateId() //autogenerate
{
City s = new City();
var i = (from x in obj.Cities
orderby x.id descending
select x).Take(1); //return max(id)
int y = 0;
foreach (var item in i)
{
y = item.id;
}
y++;
s.Cityid = "ct_" + y;
return s;
}
[HttpGet]
public ActionResult Create()
{
var country = obj.Countries.ToList();
City c = AutoGenerateId();
List<SelectListItem> li = new List<SelectListItem>();
li.Add(new SelectListItem { Text = "Select country", Value = "0" });
foreach (var item in country)
{
li.Add(new SelectListItem { Text = item.Coname, Value = item.Coid });
}
TempData["s"] = li;
TempData.Keep();
return View(c);

/* [HttpPost]
public ActionResult Create()
{

return View();

}*/
}
}
Create.cshtml(city):-
@model PAMS.City

@{
ViewBag.Title = "Create";
Layout = "~/Views/_AdminLayout.cshtml";
}

<h2>Create</h2>

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>City</h4>
<hr />
@Html.ValidationSummary(true)

<div class="form-group">
@Html.LabelFor(model => model.Cityid, new { @class = "control-label
col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Cityid)
@Html.ValidationMessageFor(model => model.Cityid)
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Cityname, new { @class = "control-
label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Cityname)
@Html.ValidationMessageFor(model => model.Cityname)
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Stateid, "Stateid", new { @class =
"control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("State", new SelectList(string.Empty, "Value",
"Text"), "--Select State--", new { style = "width:200px" })
@Html.ValidationMessageFor(model => model.Stateid)
</div>
</div>

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>
<script src="~/Scripts/jquery-1.7.1.js"></script>
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/jquery-3.2.1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#country").change(function () {
$("#State").empty();
$.ajax({
type: 'POST',
url: '@Url.Action("getstate")',
dataType: 'json',
data: { id: $("#country").val() },
success: function (states) {
$.each(states, function (i, state) {
$("#State").append('<option value="' + state.Value + '">' +
state.Text + '</option>');
});
},
error: function (ex) {
alert('Failed to retrieve states.' + ex);
}
});
return false;
})
});
</script>

You might also like