不能隐式地将int键入int [英] can not implicitly type int to string

查看:155
本文介绍了不能隐式地将int键入int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,我使用asp.net mvc5构建一个应用程序。



这里我使用了两个表。

一张表我添加了国家名称。

在其他表格中我使用这个国家名称作为drop down.also我还有一些其他字段



之后我想保存这个一张新桌子,但是我收到错误...



这里我的控制器:

  public   class  HomeController:Controller 
{
TestEntities t = new TestEntities();
//
// GET:/ Home /
public ActionResult Index()
{
var model = new \\ temp();

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

}


return 查看(模型);
}
[HttpPost]
public ActionResult save(emp model)
{
if (ModelState.IsValid)
{
var m = Rahul();
m.CountryID = model.counid; // 这里我得到的错误就像无法将int类型隐式转换为字符串
m.DOB = model.DOB;
t.Rahuls.Add(m);
t.SaveChanges();
}
return 查看( 索引,模型);

}



这是我的班级:

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web.Mvc;
使用 System.ComponentModel.DataAnnotations;


命名空间 WebApplication18.Models
{
public class emp
{

public \\ temp( ){
CountryList = new List< SelectListItem>();
}

public int id {获得; set ; }
public IList< SelectListItem> CountryList { get ; set ; }
public int counid { get ; set ; }


[DataType(DataType.Date)]
public DateTime DOB {获得; set ; }
}
}

解决方案

如果 CountryID 是一个字符串,如果你在这个字段中存储国家ID(可能是一个整数),那么 model.counid 将抛出错误。



您可以使用以下任何一种方法将int转换为字符串

model.counid.ToString();

Convert.ToString(model.counid) ;

string.Format({0},model.counid);


尝试:

 m.CountryID = Convert.ToString(model.counid)





和OP说:

引用:

我用过这个并且错误消失了..


m.CountryID = Convert.ToInt32 (model.counid);

Hi friends, i build a one application using asp.net 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);
        }
        [HttpPost]
        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;
                t.Rahuls.Add(m);
                t.SaveChanges();
            }
            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; }

   
        [DataType(DataType.Date)]
        public DateTime DOB { get; set; }
    }
}

解决方案

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
model.counid.ToString();
Convert.ToString(model.counid);
string.Format("{0}",model.counid);


Try:

m.CountryID = Convert.ToString(model.counid)



and OP says:

Quote:

i used this and error gone..


m.CountryID = Convert.ToInt32(model.counid);


这篇关于不能隐式地将int键入int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆