使用TempData将数据从Controller发送到视图

时间:2018-02-03 01:13:25

标签: asp.net asp.net-mvc

我一直试图让这个工作;当我选择部门时,它会自动填充员工字段。现在,当我将信息发送到视图页面时,它没有显示;而这有时只会发生。我尝试过使用ViewBag,结果相同。此外,viewEmployee只在会话中存储数据,因此输入的所有数据都应显示在另一个视图页面上。

控制器设置TempData

    Date date = new Date(year,month,day,hour,min);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date utc= new Date(sdf.format(date));

查看

[HttpPost]
    [AcceptVerbs(HttpVerbs.Post)]
    [AcceptParameter(Name = "Add", Value = "Add")]
    public ActionResult AddItems(Employee)
    {

  IssueDAO issuedb = new IssueDAO();

TempData["ReceivedByReps"] = issuedb.SelectEmployeesByDept(employee.department)
       return View(getEmployeee);

    }


Controller

 private getEmployee 
    {
        get
        {
            Employee employee= (Employee)Session["Employee"];
            if (employee== null)
            {
                employee= new Employee();
                Session["Employee"] = employee;
            }
            return employee;

        }

    }

 public ActionResult ViewEmployee()
    {

        return View(getEmployee);
    }

其他观点页面

 <div class="form-group">
        @Html.LabelFor(model => model.department, htmlAttributes: new { @class = "col-md-2" })

        @*@Html.EditorFor(model => model.department, new { htmlAttributes = new { @class = "form-control" } })*@
        @Html.DropDownListFor(model => model.department, new SelectList(Model.departmentsList, "Text", "Value"), new { @id = "department", @class = "form-control" })
        @*@Html.ValidationMessageFor(model => model.department, "", new { @class = "text-danger" })*@

    </div>


    <div class="form-group">
        @*@Html.LabelFor(model => model.employeeDetails, htmlAttributes: new { @class = "col-md-2" })*@
        @Html.Label("Head of Department:", htmlAttributes: new { @class = "col-md-2" })
        @if (Model.department == null)
        {
            @Html.DropDownListFor(model => model.employeeDetails.employeeNum, new SelectList(string.Empty, "Value", "Text"), new { @id = "empname", @class = "form-control" })
        }
        else
  @Html.DropDownList("ReceivedByReps", (IEnumerable<SelectListItem>)TempData["ReceivedByReps"], new { @id = "empname", @class = "form-control" })
        @Html.ValidationMessageFor(model => model.employeeDetails.employeeNum, "", new { @class = "text-danger" })


    </div>

0 个答案:

没有答案