15,887,836 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Javascript questions
View Visual Basic questions
View Python questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
Comments by Member 11233046 (Top 11 by date)
Member 11233046
18-Nov-14 10:20am
View
Nathan Minier it´s working perfectly . . . I have no word to thank you . . . Man you saved my week. for now on I´m gonna programme and spend all my night . . .
Thank you Man
Member 11233046
18-Nov-14 9:55am
View
yeah there were some errors . . . I´m gonna ran it know and see it
Member 11233046
18-Nov-14 8:41am
View
allright hope you find out the big mistake I´m making . . .
Member 11233046
18-Nov-14 8:39am
View
the exception : Additional information: The model item passed into the dictionary is of type 'FarControl.Models.AddressModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[FarControl.Models.AddressModel]'.
Hope you get know
Member 11233046
18-Nov-14 8:32am
View
and the main View
@model FarControl.Models.AddressModel
@{
ViewBag.Title = "Index";
}
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#CountryId").change(function () {
var selectedItem = $(this).val();
var ddlStates = $("#StateId");
var statesProgress = $("#states-loading-progress");
statesProgress.show();
$.ajax({
cache: false,
type: "GET",
url: "@(Url.RouteUrl("GetStatesByCountryId"))",
data: { "countryId": selectedItem },
success: function (data) {
ddlStates.html('');
$.each(data, function (id, option) {
ddlStates.append($('<option></option>').val(option.id).html(option.name));
});
statesProgress.hide();
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Failed to retrieve states.');
statesProgress.hide();
}
});
});
});
</script>
@Html.LabelFor(model => model.CountryId)
@Html.DropDownListFor(model => model.CountryId, Model.AvailableCountries)
<br />
@Html.LabelFor(model => model.StateId)
@Html.DropDownListFor(model => model.StateId, Model.AvailableStates)
<p>
// here I call the Razor Partial view
<div>
@Html.Partial("IndexStates" , Model.partialModel)
</div>
</p>
<span id="states-loading-progress" style="display: none;">Please wait..</span>
Member 11233046
18-Nov-14 8:30am
View
public ActionResult GetStates(string countryId)
{
int id = 0;
bool isValid = Int32.TryParse(countryId, out id);
if (id == 0)
throw new ArgumentException("countryId", "Must be a valid ID int");
return PartialView(db.States
.Where(x => x.Country_Id == id).ToList());
}
(like you said before)
the razor partial View
@model IEnumerable<farcontrol.models.addressmodel>
<script type="text/javascript">
$(function () {
$("#CountryId").change(function () {
var selectedItem = $(this).val();
var statesProgress = $("#states-loading-progress");
statesProgress.show();
$.ajax({
cache: false,
type: "GET",
url: "@(Url.RouteUrl("GetStates"))",
data: { "countryId": selectedItem },
success: function (data) {
tableStates.html(data);
statesProgress.hide();
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Failed to retrieve states.');
statesProgress.hide();
}
});
});
});
</script>
@if (Model != null)
{
<table>
@foreach (var states in Model)
{
<tr><td>@states.AvailableStates</td></tr>
}
</table>
}
Member 11233046
18-Nov-14 8:28am
View
to make you have a big idea I´m gonna show all . . .
hope you understand now
public class AddressModel
{
public AddressModel()
{
AvailableCountries = new List<SelectListItem>();
AvailableStates = new List<SelectListItem>();
}
public int CountryId { get; set; }
[Display(Name = "Country")]
public IList<SelectListItem> AvailableCountries { get; set; }
[Display(Name = "State")]
public int StateId { get; set; }
public IList<SelectListItem> AvailableStates { get; set; }
public IList<addressmodel> partialModel { get; set; }
}
Member 11233046
18-Nov-14 7:26am
View
Get states on my controller , like you said , there´s a error, when I put IEnumerable<string> list . . . appear a error on sql instruction , even IEnumerable<addressmodel>that I think the right way... I resolve it putting IEnumerable<states> list... doing that I have to change the razor view to @model FarControl.Models.AddressModel did you get ???
public ActionResult GetStates(string countryId)
{
int id = 0;
bool isValid = Int32.TryParse(countryId, out id);
IEnumerable<states> list = (from States in db.States
where States.Country_Id == id select States);
return PartialView(list);
}
Member 11233046
18-Nov-14 4:38am
View
I did this on controller like you said
public ActionResult GetStates(string countryId)
{
int id = 0;
bool isValid = Int32.TryParse(countryId, out id);
IEnumerable<string> list = (from States in db.States
where States.Country_Id == id select States);
return PartialView(list);
}
on my view I can do that @model IEnumerable<string> because my main view is of type @model FarControl.Models.AddressModel as I put above
my deadline is closing
Member 11233046
15-Nov-14 5:01am
View
Nathan thanks you help . . . it seems work, but I still have some issues... I can´t handle your code with mine...
on controller . . .
I have this
public class AddressController : Controller
{
Entities db = new Entities();
private IAddressRepository _repository1;
public AddressController()
: this(new AddressRepository())
{
}
public AddressController(IAddressRepository repository)
{
_repository1 = repository;
}
public ActionResult Index()
{
AddressModel model = new AddressModel();
model.AvailableCountries.Add(new SelectListItem { Text = "-Please select-", Value = "Selects items" });
var countries1 = _repository1.GetAllCountries();
//var countries = _repository.GetAllCountries();
foreach (var country in countries1)
{
model.AvailableCountries.Add(new SelectListItem()
{
Text = country.Name,
Value = country.Id.ToString()
});
}
return View(model);
}
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetStatesByCountryId(string countryId)
{
if (String.IsNullOrEmpty(countryId))
{
throw new ArgumentNullException("countryId");
}
int id = 0;
bool isValid = Int32.TryParse(countryId, out id);
var states = _repository1.GetAllStatesByCountryId(id);
var result = (from s in states
select new
{
id = s.Id,
name = s.Name
}).ToList();
return Json(result, JsonRequestBehavior.AllowGet);
}
}}
on my model . . .
<pre lang="sql">public class AddressModel
{
public AddressModel()
{
AvailableCountries = new List<SelectListItem>();
AvailableStates = new List<SelectListItem>();
}
public int CountryId { get; set; }
[Display(Name = "Country")]
public IList<SelectListItem> AvailableCountries { get; set; }
[Display(Name = "State")]
public int StateId { get; set; }
public IList<SelectListItem> AvailableStates { get; set; }
}</pre>
<pre lang="xml">public interface IAddressRepository
{
IList<Country> GetAllCountries();
IList<State> GetAllStatesByCountryId(int countryId);
}</pre>
<pre lang="xml">public class AddressRepository : IAddressRepository
{
Entities db = new Entities();
public IList<Country> GetAllCountries()
{
var query = from Countries in db.Countries
select Countries;
var content = query.ToList<Country>();
return content;
}
public IList<State> GetAllStatesByCountryId(int countryId)
{
var query = from States in db.States
where States.Country_Id == countryId
select States;
var content = query.ToList<State>();
return content;
}
}</pre>
can you give a word ? "I´m totally new, but I have to do this"
Member 11233046
14-Nov-14 9:18am
View
yeah Nathan that´s right ...
on the same page when I selected the country
please help me
Show More