0

I know this question might be asked so many times, I tried a lot to find the answer and ultimately I am throwing it here if some one can help me.

I am doing a project in ASP.NET Core MVC with a database-first approach. I have a registration form where I need to populate Country and City dropdowns to fill with data coming in through WeCareConext class.

In my controller I am doing this:

[HttpGet]
public IActionResult Register()
{
    CompanyEditViewModel model = new CompanyEditViewModel();

    var fromDatabaseEF = new SelectList(_context.Country.ToList(), "CountryId", "NameEn");
    ViewData["CompanyRegister"] = fromDatabaseEF;

    return View(model);
}

My Country model:

public partial class Country
{
    public Country()
    {
        City = new HashSet<City>();
        Currency = new HashSet<Currency>();
        HolidayType = new HashSet<HolidayType>();
        Holidays = new HashSet<Holidays>();
        Province = new HashSet<Province>();
        StaffInfo = new HashSet<StaffInfo>();
    }

    public int CountryId { get; set; }
    public string NameAr { get; set; }
    public string NameEn { get; set; }
    public string NameFr { get; set; }
    public string IsoCode2 { get; set; }
    public string IsoCode3 { get; set; }
    public string RegionAr { get; set; }
    public string Region { get; set; }
    public byte? PostcodeRequired { get; set; }
    public bool? IsActive { get; set; }
    public byte? IsAvailable { get; set; }
    public string MobileMask { get; set; }
    public byte? IsDeleted { get; set; }
    public int? BaseCharges { get; set; }
    public int? MobileNumberLength { get; set; }
    public string CountryCode { get; set; }
    public byte[] IconImage { get; set; }
    public string PickerListAr { get; set; }
    public string PickerListEn { get; set; }
    public string PickerListFr { get; set; }

    public virtual ICollection<City> City { get; set; }
    public virtual ICollection<Currency> Currency { get; set; }
    public virtual ICollection<HolidayType> HolidayType { get; set; }
    public virtual ICollection<Holidays> Holidays { get; set; }
    public virtual ICollection<Province> Province { get; set; }
    public virtual ICollection<StaffInfo> StaffInfo { get; set; }
}

My CompanyEditViewModel class:

public class CompanyEditViewModel
{
    [Display(Name = "Company Number")]
    public string CompanyId { get; set; }

    [Required]
    [Display(Name = "Company Name")]
    [StringLength(75)]
    public string Name { get; set; }

    [Required]
    [Display(Name = "Country")]
    public string CountryId { get; set; }
    public IEnumerable<SelectListItem> Countries { get; set; }

    [Required]
    [Display(Name = "City")]
    public string SelectedCityCode { get; set; }
    public IEnumerable<SelectListItem> Cities { get; set; }
}

Unfortunately, this returns an error:

System.ArgumentException: 'Format of the initialization string does not conform to specification starting at index 0.'

I am trying different approaches but not solving my issue. Can anyone suggest some code which can help me and I can also select country and then populate city dropdown according to the CountryID without page reload ? Thanks in advance.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jugnu Khan
  • 63
  • 3
  • 9
  • see https://stackoverflow.com/questions/8243008/format-of-the-initialization-string-does-not-conform-to-specification-starting-a – Johan Maes May 01 '20 at 08:24
  • Thanks for your recommendation but this one is not relevant to my issue. – Jugnu Khan May 01 '20 at 09:02
  • What's exactly throwing the error? is it the database or the action? Also show your `Country` model. – Anton Toshik May 01 '20 at 09:11
  • I have updated with both the models. Please review and advise because i am struggling since yesterday and messing up now with code. – Jugnu Khan May 01 '20 at 09:18
  • System.ArgumentException: 'Format of the initialization string does not conform to specification starting at index 0.' – Jugnu Khan May 01 '20 at 09:22
  • Try getting countries in object like `var countries = _context.Country.ToList()`. It seems to be connection string issue. If you are able to fetch countries then try creating dummy list of countries and pass it to `new SelectList(...)`. This way you will be able to find from where exactly exception comes. – Karan May 01 '20 at 09:29

0 Answers0