【发布时间】:2016-05-24 10:40:02
【问题描述】:
关于这个问题,我已经提到了几个问题(Q1,Q2),但我认为我的问题略有不同。
我已经创建了一个登录页面,成功登录后应该会打开主控制器的索引视图。由于我的索引视图需要模型来绑定表数据,因此我需要将模型对象与登录按钮操作中的视图一起传递。我希望你能理解这个场景。
这是我的登录控制器:
public ActionResult Login() //Get Method for Login
{
return View("Login");
}
[HttpPost] // Post Method on Login
public ActionResult Defaults(string username, string password)
{
FloorFactor ff = new FloorFactor(); // tried to create model object to pass in view
try
{
Login_Details login_detail = db.Login_Details.Single(x => (x.UserName == username) && (x.Password == password));
if (login_detail != null)
{
return View("../Home/Index",ff ); // need to get view Index of home controller.
}
}
catch { }
return View("Login");
}
Index.cshtml 的必需部分
@model IEnumerable<Pricing_Tool_Test.FloorFactor>
@{
ViewBag.Title = "Index";
}
我知道模型需要 Iennumerable 类型的模型对象,但在我的 Defaults 方法中它不允许我传递模型的 Iennumerable 对象。
任何帮助我怎样才能做到这一点?
编辑:
两个控制器不同。登录按钮需要加载 Homecontroller 的视图。它会产生异常,如图所示。如果我评论该行而不是生成上述错误。 :
编辑 2:主页/创建编码
public PartialViewResult Create()
{
return PartialView("_CreatePartial");
}
// POST: Home/Create
[HttpPost]
public ActionResult Create([Bind(Include = "FloorFactorPercentage,FromDate,ToDate")]FloorFactor floorfactor)
{
if (ModelState.IsValid)
{
db.FloorFactors.Add(floorfactor);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(floorfactor);
}
【问题讨论】:
-
在您的视图中只需使用@model Pricing_Tool_Test.FloorFactor,因为您正在传递单个元素,因此不需要 IEnumerable
-
成功登录后,它会在 Index.cshtml 上加载目录,因此它不是单个元素,因此需要 IEnumerable
-
您的代码只是传递了一个名为 'ff' 的空对象
-
@CodeCaster 见编辑。我认为没有必要对帖子投反对票,因为您提到的问题在我的搜索结果中没有找到。链接问题的标题有很大不同。当您没有看到任何搜索努力或不清楚时,会使用 Downvote