sms*_*are 2 c# asp.net-mvc tempdata entity-framework-4
我有这个问题,从TempData探测整数,因为它看到DempData ["sth"]作为对象,而不是整数本身.这是我的Create方法我将我的整数发送到TempData:
public ActionResult Create(int CustomerId, int qSetId, int Count)
{
qSet qset = db.qSets.Find(qSetId);
TempData["qSetId"] = qset.Id;
Customer customer = db.Customers.Find(CustomerId);
TempData["CustomerId"] = customer.Id;
List<Relation> relations = db.Relations.Where(r => r.qSetId.Equals(qSetId)).ToList<Relation>();
Question question = new Question();
List<Question> questions = new List<Question>();
foreach (Relation relation in relations)
{
question = db.Questions.Find(relation.QuestionId);
if (questions.Contains<Question>(question).Equals(false))
questions.Add(question);
}
if (questions.Count<Question>().Equals(Count).Equals(false))
{
TempData["QuestionId"] = questions[Count].Id;
TempData["QuestionType"] = questions[Count].Type;
ViewBag["QuestionContent"] = questions[Count].Content;
TempData["Count"] = Count + 1;
return View();
}
else
{
return RedirectToAction("ThankYou");
}
}
Run Code Online (Sandbox Code Playgroud)
这是另一种方法,探测这些数据:
[HttpPost]
public ActionResult Create(Answer answer)
{
answer.QuestionId = TempData["QuestionId"];
answer.CustomerId = TempData["CustomerId"];
if (ModelState.IsValid)
{
db.Answers.Add(answer);
db.SaveChanges();
return RedirectToAction("Create", new { CustomerId = TempData["CustomerId"], qSetId = TempData["qSetId"], Count = TempData["Count"] });
}
ViewBag.CustomerId = new SelectList(db.Customers, "Id", "eAdress", answer.CustomerId);
ViewBag.QuestionId = new SelectList(db.Questions, "Id", "Name", answer.QuestionId);
return View(answer);
}
Run Code Online (Sandbox Code Playgroud)
错误出现在:
answer.QuestionId = TempData["QuestionId"];
answer.CustomerId = TempData["CustomerId"];
Run Code Online (Sandbox Code Playgroud)
并且像这样:
无法将类型'object'隐式转换为'int'.存在显式转换(您是否错过了演员?)
有帮助吗?
对此的解决方案称为"拆箱".它是从object到int的直接转换:
answer.QuestionId = (int)TempData["QuestionId"];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5364 次 |
| 最近记录: |