简体   繁体   English

详细操作在ASP.NET MVC 4中不起作用

[英]Details action not working in asp.net mvc 4

I'm trying to make a website with asp.net mvc 4 & entity framework 6 where user can see their profile details by clicking Profile button. 我正在尝试使用asp.net mvc 4和实体框架6创建一个网站,通过单击“ Profile按钮,用户可以查看其个人资料详细信息。 But everytime I try to Login I get error like below. 但是每次我尝试Login ,都会出现如下错误。 If I comment out the ProfileView section then I can login. 如果我注释掉ProfileView部分,则可以登录。 My code is below, 我的代码如下

Controller 调节器

public ActionResult ProfileView(int UserId=0)
{
  UserInfo profile = db.UserInfoes.Find(UserId);
  if (Session["UserBOID"] != null)
  {
    return View(profile);
  }
  else
  {
    return RedirectToAction("Login");
  }
}

View 视图

@model ABCoLtd.Models.MkistatVsUserLogin
@if (@Session["UserBOID"] != null)
{
  <li>Welcome, <b>@Session["UserBOID"].ToString()</b></li>
  foreach(var item in Model)
  {
    <li><a class="btn btn-info" href="@Url.Action("ProfileView", "Home", new { UserId=item.UserId })" target="_blank"><b>Profile</b></a></li>
  }
}

Custom Model to use different table under same model 自定义模型以在同一模型下使用不同的表

public class MkistatVsUserLogin
{
    public IEnumerable<UserInfo> UserInfo { get; set; }
    public IEnumerable<mkistat> mkistats { get; set; }
}

Is there something wrong in my code? 我的代码有问题吗? If so, please give me a solution since I need this help badly. 如果是这样,请给我一个解决方案,因为我非常需要此帮助。 Your help will be appreciated. 您的帮助将不胜感激。 Tnx. TNX。

UPDATES 更新

Error 错误

Compilation Error Description: An error occurred during the compilation of a resource required to service this request. 编译错误描述:在编译为满足此请求所需的资源期间发生错误。 Please review the following specific error details and modify your source code appropriately. 请查看以下特定的错误详细信息,并适当地修改您的源代码。

Compiler Error Message: CS1579: foreach statement cannot operate on variables of type 'ABCoLtd.Models.MkistatVsUserLogin' because 'ABCoLtd.Models.MkistatVsUserLogin' does not contain a public definition for 'GetEnumerator' 编译器错误消息:CS1579:foreach语句无法对'ABCoLtd.Models.MkistatVsUserLogin'类型的变量进行操作,因为'ABCoLtd.Models.MkistatVsUserLogin'不包含'GetEnumerator'的公共定义

Source Error: Line 37: foreach(var item in Model) 源错误:行37:foreach(模型中的var项)

您仅将一个对象从控制器传递给视图,因此如果要在其上循环,则需要将其更改为collection。

  UserInfo profile = db.UserInfoes.Find(UserId);//This is only one object

Change Model to Model.UserInfo , like: Model更改为Model.UserInfo ,例如:

 @model ABCoLtd.Models.MkistatVsUserLogin
 @if (@Session["UserBOID"] != null)
 {
    <li>Welcome, <b>@Session["UserBOID"].ToString()</b></li>
    foreach(var item in Model.UserInfo) <<---
    {
       <li><a class="btn btn-info" href="@Url.Action("ProfileView", "Home", new { UserId=item.UserId })" target="_blank"><b>Profile</b></a></li>
    }
 }

You have to use the collection Model.UserInfo in your loop instead of the view model itself. 您必须在循环中使用集合Model.UserInfo ,而不是视图模型本身。

First of all you are passing UserInfo object to view 首先,您要传递UserInfo对象进行查看

UserInfo profile = db.UserInfoes.Find(UserId);

Problem is that your view expects MkistatVsUserLogin model 问题是您的视图需要MkistatVsUserLogin模型

@model ABCoLtd.Models.MkistatVsUserLogin

You have to pass MkistatVsUserLogin object to view in ProfileView ActionResult and make UserInfo List to be able to add objects to collection. 您必须传递MkistatVsUserLogin对象以在ProfileView ActionResult中查看,并使UserInfo List能够将对象添加到集合中。

public class MkistatVsUserLogin
{
  public List<UserInfo> UserInfo { get; set; }
  public IEnumerable<mkistat> mkistats { get; set; }
}

public ActionResult ProfileView(int UserId=0)
{
  UserInfo profile = db.UserInfoes.Find(UserId);
  MkistatVsUserLogin model = new MkistatVsUserLogin();
  model.UserInfo = new List<UserInfo>();
  model.UserInfo.Add(profile);
  if (Session["UserBOID"] != null)
  {
    return View(model);
  }
  else
  {
    return RedirectToAction("Login");
  }
}

Now second problem is in your view. 现在您认为第二个问题。 You are looping MkistatVsUserLogin model and you should loop UserInfo collection 您正在循环MkistatVsUserLogin模型,应该循环UserInfo集合

  foreach(var item in Model.UserInfo)
  {
    <li><a class="btn btn-info" href="@Url.Action("ProfileView", "Home", new { UserId=item.UserId })" target="_blank"><b>Profile</b></a></li>
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM