I am trying to Render a view which consists of just a DropDownList inside another View in ASP.NET MVC but for some reason the Model is Null. I think it's because I am only calling the view and the Controller isn't being called at all. Where am I going wrong? maybe my phone approach is incorrect.
Dropbox View
@model MyWatch.Models.Countries
@{
ViewBag.Title = "CountrySearch";
Layout = null;
if (Model != null)
{
Html.DropDownListFor(m => m.countries, new SelectList(Model.countries.Select(s => new SelectListItem { Text = s.name, Value = s.code })));
}
else
{
Html.DropDownListFor(m => m.countries, new SelectList(new List<String>()));
}
}
Main View
<div class="form-group">
@Html.LabelFor(m => m.Country, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@{Html.RenderPartial("Search/CountrySearch");}
</div>
</div>
Controller
public ActionResult CountrySearch()
{
try
{
using (StreamReader streamReader = new StreamReader("C:\\Users\\Alex Combe\\Documents\\Visual Studio 2015\\Projects\\MyWatch\\MyWatch\\App_Data\\CountryList.json"))
{
string json = streamReader.ReadToEnd();
Countries countries = new Countries();
countries.countries = JsonConvert.DeserializeObject<IList<Country>>(json);
return View(countries);
}
}
catch (FileNotFoundException e)
{
Console.WriteLine(e.StackTrace);
return View(new Countries());
}
}
}
Model
namespace MyWatch.Models
{
public class Country
{
public string name { get; set; }
public string code { get; set; }
public SelectList selectList { get; set; }
}
public class Countries
{
public IList<Country> countries;
}
}