通过ViewBag传递模型对象 [英] Passing a model object through ViewBag

查看:51
本文介绍了通过ViewBag传递模型对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以通过ViewBag传递模型对象.我尝试了以下代码,但是以某种方式在我的View中显示的只是模型的路径.

I was wondering if it is possible to pass a model object through ViewBag. I tried the following codes but somehow in my View, it is only the path of the model which is displayed.

控制器:

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传递模型?

The Fname and EmployeeId is correctly displayed but not the Employee itself. Am I missing something here or it is simply not possible to pass a model through ViewBag?

推荐答案

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

It would be better to create a view model to pass to the view:

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

然后有一个控制器创建视图模型:

Then have a controller create the view model:

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

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

And then display the view model in the view:

@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()方法,因此将调用继承的对象实现,该实现将写出完整的名称空间和类名称(这是我在说路径时所假定的含义).

With your current implementation, what you are trying to achieve when you call @ViewBag.Employee in the view, is to write the model out as a string representation. With the current implementation, to convert the model to a string, the ToString() method of the model is called. As you (probably) have not overridden the ToString() method, the inherited object implementation is called instead which writes out the complete namespace and class name (which is what I assume you mean when you say path).

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

To correct your current solution, you can add an implementation of ToString() to your Employee class. For example:

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

这篇关于通过ViewBag传递模型对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆