Click here to Skip to main content
15,943,082 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends, i build a one application using mvc5.

here i have used two tables.
one table i add country names.
in other table i used this country names as drop down.also i have some other field also

after i want to save this in a new table but i get error...

here my controller:
public class HomeController : Controller
        TestEntities t = new TestEntities();
        // GET: /Home/
        public ActionResult Index()
            var model = new emp();

            var countryList = t.Prabs.ToList();
            foreach (var country in countryList)
                model.CountryList.Add(new SelectListItem()
                    Text = country.Name,
                    Value = country.ID.ToString(),


            return View(model);
        public ActionResult save(emp model)
            if (ModelState.IsValid)
                var m = new Rahul();
                m.CountryID = model.counid; // here i got error like cannot implicitly convert type int to string
                m.DOB = model.DOB;
            return View("Index",model);


Here is my class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;

namespace WebApplication18.Models
    public class emp

        public emp() {
        CountryList = new List<SelectListItem>();
        public int id { get; set; }
        public IList<SelectListItem> CountryList { get; set; }   
        public int counid { get; set; }

        public DateTime DOB { get; set; }
Updated 3-Mar-14 1:42am
Tom Marvolo Riddle 3-Mar-14 7:26am    
m.CountryID = Convert.ToString(model.counid);
prabu19 3-Mar-14 7:35am    
i used this and error gone..
but now run means it shows exception in index action method like var countryList = t.Prabs.ToList();
Tom Marvolo Riddle 3-Mar-14 7:38am    
what exception you are getting?
prabu19 3-Mar-14 7:57am    
An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: The 'ID' property on 'Prab' could not be set to a 'System.Int32' value. You must set this property to a non-null value of type 'System.String'.

this error appers on here " var countryList = t.Prabs.ToList();"
Tom Marvolo Riddle 3-Mar-14 23:34pm    
sorry for the late reply. you are accessing the object .we don't know what wrong in it
TestEntities t = new TestEntities();
Use debugger to find

If CountryID is a string and if you are storing country id (perhaps an integer) in this field, then model.counid will throw error.

You can use any of the following to convert int to string
Share this answer
prabu19 3-Mar-14 7:39am    
now error gone. of i run means it redirect to browser widow and come to controller coding of this line "var countryList = t.Prabs.ToList();"
m.CountryID = Convert.ToString(model.counid)

and OP says:
i used this and error gone..
Share this answer
m.CountryID = Convert.ToInt32(model.counid);
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900