[英]Cannot implicitly convert type 'object' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)
[英]I got cannot implicitly convert type 'object' to 'system.guid'? An Explicit conversion exists (are you missing a cast?) error
我的控制器
嗨,我無法將類型對象隱式轉換為system.guid嗎? 此行taxinfotaxfiled.TaxFieldID = i中存在顯式轉換(是否缺少強制轉換?)錯誤。 尤其是在“ i”中,有人可以給我解決方案嗎?
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; }
}
這是由於ArrayList
不是通用類,因此對它存儲的數據一無所知。 因此, i
是一個Object
,您不能將Object
隱式轉換為Guid
。 如錯誤消息所提示,您可以使用一個顯式的taxinfotaxfiled.TaxFieldID = (Guid)i
,如taxinfotaxfiled.TaxFieldID = (Guid)i
。 它可能不起作用,因為ArrayList實際上包含字符串,而不包含Guid,在這種情況下,您要么需要將填充ArrayList
的方式更改為
Alist.Add(Guid.Parse(<value>));
或將您從ArrayList
獲取值的方式替換為:
taxinfotaxfiled.TaxFieldID = Guid.Parse(i.ToString());
或者,甚至更好的是,將ArrayList
替換為通用類List<Guid>
,在這種情況下,集合將知道其持有的對象的類型,並且您不需要任何強制類型轉換。
換線
taxinfotaxfiled.TaxFieldID = i.Value;
對此
taxinfotaxfiled.TaxFieldID = new Guid(i.Value.ToString());
您需要使用以下代碼替換代碼:
Alist.Add(Guid.Parse("FD713788-B5AE-49FF-8B2C-F311B9CB0CC4"));
其余的也一樣。 使用List<Guid>
代替ArrayList
也是一個更好的主意。
用這個:
Guid.Parse(((Telerik.Windows.Controls.GridView.GridViewCell)e.Row.Cells[cellIndex]).Value.ToString());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.