C# 我无法隐式转换类型';对象';至';system.guid';?存在显式转换错误(是否缺少强制转换?)
我的控制器 Hi I Get无法将类型对象隐式转换为system.guid?存在显式转换(是否缺少转换?)。此行出现错误TaxInfoTaxField.TaxFieldID=i。特别是在“我”中,有人能给我答案吗C# 我无法隐式转换类型';对象';至';system.guid';?存在显式转换错误(是否缺少强制转换?),c#,C#,我的控制器 Hi I Get无法将类型对象隐式转换为system.guid?存在显式转换(是否缺少转换?)。此行出现错误TaxInfoTaxField.TaxFieldID=i。特别是在“我”中,有人能给我答案吗 public ActionResult Create(TaxInfoTaxFiled taxinfotaxfiled) { ArrayList Alist = new ArrayList(); { Alist.Add("FD
public ActionResult Create(TaxInfoTaxFiled taxinfotaxfiled)
{
ArrayList Alist = new ArrayList();
{
Alist.Add("FD713788-B5AE-49FF-8B2C-F311B9CB0CC4");
Alist.Add("FD713788-B5AE-49FF-8B2C-F311B9CB0CC4");
Alist.Add("64B512E7-46AE-4989-A049-A446118099C4");
Alist.Add("376D45C8-659D-4ACE-B249-CFBF4F231915");
Alist.Add("59A2449A-C5C6-45B5-AA00-F535D83AD48B");
Alist.Add("03ADA903-D09A-4F53-8B67-7347A08EDAB1");
Alist.Add("2F405521-06A0-427C-B9A3-56B8931CFC57");
}
if (ModelState.IsValid)
{
taxinfotaxfiled.TaxInfoTaxFieldID = Guid.NewGuid();
foreach (var i in Alist)
{
taxinfotaxfiled.TaxFieldID = i.Value;
}
db.TaxInfoTaxFileds.Add(taxinfotaxfiled);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(taxinfotaxfiled);
}
我的模型
public partial class TaxInfoTaxFiled
{
public System.Guid TaxInfoTaxFieldID { get; set; }
public Nullable<System.Guid> TaxInfoID { get; set; }
public Nullable<System.Guid> TaxFieldID { get; set; }
public Nullable<System.Guid> FieldTypeID { get; set; }
public string FieldValue { get; set; }
}
public partial class TaxField
{
public System.Guid TaxFieldID { get; set; }
public string DisplayName { get; set; }
public string PrintName { get; set; }
}
公共部分类出租车
{
public System.Guid TaxInfoTaxFieldID{get;set;}
公共可空TaxInfo{get;set;}
公共可空TaxFieldID{get;set;}
公共可为空的FieldTypeID{get;set;}
公共字符串字段值{get;set;}
}
公共部分类分类域
{
public System.Guid TaxFieldID{get;set;}
公共字符串DisplayName{get;set;}
公共字符串PrintName{get;set;}
}
更改行
taxinfotaxfiled.TaxFieldID = i.Value;
对此
taxinfotaxfiled.TaxFieldID = new Guid(i.Value.ToString());
换线
taxinfotaxfiled.TaxFieldID = i.Value;
对此
taxinfotaxfiled.TaxFieldID = new Guid(i.Value.ToString());
您需要将代码替换为以下内容:
Alist.Add(Guid.Parse("FD713788-B5AE-49FF-8B2C-F311B9CB0CC4"));
其他guid也一样。使用
列表
而不是数组列表
可能是一个更好的主意。您需要用以下内容替换代码:
Alist.Add(Guid.Parse("FD713788-B5AE-49FF-8B2C-F311B9CB0CC4"));
其他guid也一样。使用
List
而不是ArrayList
可能是一个更好的主意。这是因为ArrayList
不是泛型类,因此不知道它存储的数据。因为i
是一个对象
,所以不能将对象
隐式转换为Guid
。如错误消息所示,您可以使用显式强制转换,如taxinfotaxfield.TaxFieldID=(Guid)i
中所示。这可能不起作用,因为您的ArrayList实际上包含字符串,而不是GUID,在这种情况下,您需要将填充ArrayList的方式更改为
Alist.Add(Guid.Parse(<value>));
或者,更好的是,用泛型类列表替换ArrayList
,在这种情况下,集合将知道它所持有的对象的类型,并且您将不需要任何强制转换。这是因为ArrayList
不是泛型类,因此不知道它存储的数据。因为i
是一个对象
,所以不能将对象
隐式转换为Guid
。如错误消息所示,您可以使用显式强制转换,如taxinfotaxfield.TaxFieldID=(Guid)i
中所示。这可能不起作用,因为您的ArrayList实际上包含字符串,而不是GUID,在这种情况下,您需要将填充ArrayList的方式更改为
Alist.Add(Guid.Parse(<value>));
或者,更好的方法是,将数组列表
替换为泛型类列表
,在这种情况下,集合将知道它所持有的对象的类型,并且您将不需要任何强制转换。使用以下方法:
Guid.Parse(((Telerik.Windows.Controls.GridView.GridViewCell)e.Row.Cells[cellIndex]).Value.ToString());
使用以下命令:
Guid.Parse(((Telerik.Windows.Controls.GridView.GridViewCell)e.Row.Cells[cellIndex]).Value.ToString());
ToString()不是必需的;我已经是一根绳子了。此外,这些类型的答案在附有解释时会更好。@RobertHarvey ArrayList中的元素是object而不是string。因此,异常是Guid的对象。如果他在循环var i中写入,它将返回object!不管怎样,你可能想试一下。我很确定隐式演员阵容已经成功了。如果没有,那就糟糕了,但是有很多地方有。@mybirthname是正确的,您确实需要一个ToString()
,或者强制转换为字符串
。无论哪种方式,都不能将对象传递到Guid
的构造函数中,或者ToString()不是必需的;我已经是一根绳子了。此外,这些类型的答案在附有解释时会更好。@RobertHarvey ArrayList中的元素是object而不是string。因此,异常是Guid的对象。如果他在循环var i中写入,它将返回object!不管怎样,你可能想试一下。我很确定隐式演员阵容已经成功了。如果没有,那就糟糕了,但是有很多地方有。@mybirthname是正确的,您确实需要一个ToString()
,或者强制转换为字符串
。无论哪种方式,都不能将对象传递到Guid
的构造函数中