简体   繁体   中英

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. But everytime I try to Login I get error like below. If I comment out the ProfileView section then I can login. 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.

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'

Source Error: Line 37: foreach(var item in Model)

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

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

Change Model to Model.UserInfo , like:

 @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.

First of all you are passing UserInfo object to view

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

Problem is that your view expects MkistatVsUserLogin model

@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.

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

  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>
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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