【问题标题】:Passing a model object through ViewBag通过 ViewBag 传递模型对象
【发布时间】:2015-10-02 12:15:39
【问题描述】:

我想知道是否可以通过 ViewBag 传递模型对象。我尝试了以下代码,但不知何故在我的视图中,它只是显示的模型的路径。

控制器:

public ActionResult Tempo()
{
    DateTime date1 = new DateTime(1990, 1, 1);
    Employee emp = new Employee(3, "fara", "hass", date1, 56.6m, 0);
    ViewBag.EmployeeId = emp.EmployeeID;
    ViewBag.Fname = emp.Fname;
    ViewBag.Employee = emp;
}

查看:

@{
    ViewBag.Title = "Tempo";
}

@model LayoutProject.Models.Employee

<h2>Tempo</h2>
<div>
    <h4>ViewBag</h4>
    <br />
    EmployeeID: @ViewBag.EmployeeId
    <br />
    Fname: @ViewBag.Fname
    <br />
    Employee : @ViewBag.Employee
</div>

正确显示了 Fname 和 EmployeeId,但未正确显示 Employee 本身。我是否在这里遗漏了什么,或者根本无法通过 ViewBag 传递模型?

【问题讨论】:

  • 您正在使用ViewBag 传递模型,但@ViewBag.Employee 显示.ToString()Employee 无需包含ViewBag.EmployeeId = emp.EmployeeID;ViewBag.Fname = emp.Fname; - 您可以使用@ 987654329@ 和视图中 - EmployeeID: @((Employee)ViewBag.emp).EmployeeId 等。但是为什么要这样做而不是将模型传递给视图 - return View(emp)
  • @tabby - 您希望在 Employee 字段中看到什么?
  • @Stephen:所以我不能使用 '@ViewBag.emp' 来显示整个模型及其属性?
  • @Dangerous:我认为 ViewBag.emp 会显示整个模型。这可能吗?
  • @tabby - 我希望您希望将模型显示为字符串,但您需要指定该字符串的格式。您应该覆盖员工对象的 ToString() 方法或在包含信息的视图模型中创建一个字段。

标签: asp.net-mvc-3 viewbag


【解决方案1】:

最好创建一个视图模型来传递给视图:

public class TempoViewModel
{
    public int EmployeeId { get; set; }
    public string FirstName { get; set; }

    public string LastName { private get; set; }
    public DateTime EmployeeStartDate { private get; set; }
    //any other properties here that make up EmployeeInformation

    public string EmployeeInformation
    { 
        get
        {
            //Format your employee information in the way you intend for the view
            return string.Format("Employee: {0}, {1}, {2}", this.FirstName, this.LastName, this.EmployeeStartDate);
        }
    }
}

然后让控制器创建视图模型:

public ViewResult Tempo()
{
    employee = //logic to retrieve employee information

    //map model to viewmodel
    var viewModel = new TempoViewModel()
    {
        EmployeeId = employee.EmployeeID,
        FirstName = employee.Fname,
        LastName = employee.Lname, //or set whatever properties are required to display EmployeeInformation
        EmployeeStartDate = employee.StartDate,
    };

    return View(viewModel);
}

然后在视图中显示视图模型:

@model TempoViewModel

@{
    ViewBag.Title = "Tempo";
}

<h2>Tempo</h2>
<div>
    <h4>Tempo Employee Information</h4>
    <br />
    EmployeeID: @Model.EmployeeId @* Do you really want to display the employee id? *@
    <br />
    Fname: @Model.FirstName
    <br />
    Employee: @Model.EmployeeInformation
</div>

更新:

使用您当前的实现,当您在视图中调用@ViewBag.Employee 时,您想要实现的是将模型写为字符串表示形式。在当前实现中,要将模型转换为字符串,需要调用模型的 ToString() 方法。由于您(可能)没有覆盖 ToString() 方法,因此会调用继承的对象实现,它会写出完整的命名空间和类名(当您说路径时,我假设您的意思是)。

要更正您当前的解决方案,您可以将 ToString() 的实现添加到您的 Employee 类。例如:

public class Employee
{
    public Employee(int employeeId, string firstName, string lastName)
    {
        this.EmployeeId = employeeId;
        this.FName = firstName;
        this.LName = lastName;
        //etc
    }

    public int EmployeeId { get; set; }
    public string FName { get; set; }
    public string LName { get; set; }

    public override string ToString()
    {
        //Implement your required string representation of the object here
        return string.Format("EmployeeId: {0}, FName: {1}, LName: {2}", EmployeeId, FName, LName);
    }
}

【讨论】:

  • @tabby - 你是如何使用这个解决方案的?
  • 不,这不是我想要的。在这里,您没有使用 ViewBag。我想要的是使用 ViewBag 将模型从控制器传输到视图。在控制器中执行“ViewBag.Employee = emp”并在视图中执行“Employee:@ViewBag.Employee”时,我得到的是我的模型的路径。我想显示模型本身,而不是路径。我这样做的方式有什么问题?
  • @tabby - 很公平,我没有直接回答你的问题。我已经更新了我的答案。
  • 这就是我想要的!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-25
  • 1970-01-01
  • 1970-01-01
  • 2020-03-04
  • 2013-03-28
  • 1970-01-01
相关资源
最近更新 更多