Tech
Forums
Jobs
Books
Events
Videos
Conference
Annual Conference
Bcrypt
Ai Conference
Live
More
Interviews
Certification
Training
Career
Members
News
Blogs
Contribute
An Article
A Blog
A Video
An Ebook
An Interview Question
Register
Login
8
Answers
Retrieve wrong city for district
Ask a question
Ask a question
Upload Source Code:
Select only zip and rar file.
ahmed salah
8y
233
1
Reply
in image above it retrieve wrong city for district
when i need to edit for id 4 in table district as image
it give me wrong city
country name and district name is retrieve true
edit view District
@model WebCourse.Models.Destrict
@{
Layout =
null
;
}
<!DOCTYPE html>
<html>
<head>
<meta name=
"viewport"
content=
"width=device-width"
/>
<title>Edit</title>
<script src=
"~/scripts/jquery-1.10.2.js"
></script>
<script>
$(function () {
$(
"#ctry"
).change(function () {
$(
"#citylist"
).empty();
var x = $(
this
).val();
$.ajax({
url:
"/empcourse/getcitybyid"
,
data: { id: x },
success:function(res)
{
$.each(res, function (i, e) {
$(
"#citylist"
).append(
"<option value='"
+ e.Id +
"'>"
+ e.CityName +
"<option>"
)
});
}
});
});
$(
"#ctry"
).change();
});
</script>
</head>
<body>
<div>
@
using
(Html.BeginForm())
{
<div>
CountryName:@Html.DropDownList(
"ctry"
)
</div>
<div>
City:<select id=
"citylist"
name=
"CityId"
></select>
<br />
DistrictName:@Html.TextBoxFor(a => a.DistrictName)
</div>
<input type=
"submit"
/>
}
</div>
</body>
</html>
in
district controller
in
edit (
get
)
namespace
WebCourse.Controllers
{
public
class
DistrictController : Controller
{
mycourseEntities db =
new
mycourseEntities();
public
ActionResult Edit(
int
id)
{
Destrict D = db.Destricts.Find(id);
int
ctryold = D.City.Country.Id;
ViewBag.ctry =
new
SelectList(db.Countries.ToList(),
"Id"
,
"CountryName"
, ctryold);
return
View(D);
}
public
JsonResult getcitybyid(
int
id)
{
db.Configuration.ProxyCreationEnabled =
false
;
return
Json(db.Cities.Where(a => a.CountryId == id), JsonRequestBehavior.AllowGet);
}
}
}
Post
Reset
Cancel
Answers (
8
)
Next Recommended Forum
how to show dynamic Content in partial view MVC Razor view e
How to Update exist course id in EmployeeCourse table