0

我在 asp.net mvc 中做一个项目。我想在视图中显示特定人的详细信息。我必须加入 2 个表才能显示数据。为此我做了:

控制器:

[HttpGet]
public ViewResult DisplayData()  
{  
    ViewBag.Designation1up = new SelectList(db.Designations, "Designation1up", "DesignationInternal", "DesignationExternal");  
    return View();  
}  

[HttpPost]  
public ActionResult DisplayData(Employee emp)  
{  
    try  
    {  
        object s = Session["EmployeeID"];  
        var sessval = s.ToString();  
        var data1 = (from e in db.Employees.Where(c => c.EmployeeID == sessval) join d in db.Designations on e.Designation1up equals d.Designation1up select e).SingleOrDefault();  
        return View(data1);  
    }  
    catch (Exception e)  
    {  
    }  
    ViewBag.Designation1up = new SelectList(db.Designations, "Designation1up", "DesignationInternal",emp.Designation1up);  
    return View(emp);     
}

风景:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<ResourceTracking.ViewModel.AdminDetailsModel>" %> 
<!DOCTYPE html> 
<html> 
  <head runat="server"> 
    <title>DisplayData</title> 
  </head> 
  <body> 
    <fieldset> 
      <legend>AdminDetailsModel</legend> 
      <div class="display-label">EmployeeID</div> 
      <div class="display-field"> <%: Html.DisplayFor(model => model.EmployeeID) %> </div> 
      <!--...(same DIV 4 other fields)-->
   </fieldset>  
  </body> 
</html> 

我的问题是,当我调试代码时,编译器没有进入该HttpPost方法。它只是调试HttpGet方法并给出输出,但除非HttpPost运行,否则输出将不正确。我该怎么办?

4

1 回答 1

0

我认为您对 ASP.NET MVC 的工作方式有些困惑。通常要在视图上显示数据,您将调用 HttpGet 操作 (DisplayData),创建一个模型,用相关数据(员工)填充它并显示它。

像这样的东西(未经测试)

[HttpGet]
public ViewResult DisplayData()  
{  
    ViewBag.Designation1up = new SelectList(db.Designations, "Designation1up", "DesignationInternal", "DesignationExternal");  

    SomeModel model = new SomeModel();

    object s = Session["EmployeeID"];  
    if (s != null)
    {
        var employeeId = s.ToString();  

        model.EmployeeData = GetEmployeeData(employeeId);
    }

    return View(model);  
}  

private Employee GetEmployeeData(string employeeId)  
{  
    return (from e in db.Employees.Where(c => c.EmployeeID == employeeId) 
            join d in db.Designations 
                on e.Designation1up equals d.Designation1up 
            select e).SingleOrDefault();  
}
于 2013-02-25T13:37:13.017 回答