2013-02-25 76 views
0

我正在做一個asp.net mvc項目。我想在視圖中顯示特定人員的詳細信息。我必須加入兩個表來顯示數據。對於我所做的:從控制器顯示特定用戶數據以查看

控制器:

[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,否則輸出將不正確。我應該怎麼做?

+0

你能顯示相應的看法? – 2013-02-25 13:03:36

+0

<%@頁面語言= 「C#」 繼承= 「System.Web.Mvc.ViewPage 」 %> <!DOCTYPE HTML> <頭RUNAT = 「服務器」> DisplayData

AdminDetailsModel
EmployeeID
<%: Html.DisplayFor(model => model.EmployeeID) %>
...(同DIV 4 OTH呃字段)
2013-02-25 13:13:24

+0

您是否期望'Html.DisplayFor'調用'HttpPost'方法? – 2013-02-25 13:26:57

回答

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(); 
} 
+0

的一些基本概念其實,我是新的asp .net mvc3,所以我想學習它。在任何教程中我都沒有想到這個。感謝Tyriar的回覆,這非常支持。非常感謝你 – 2013-02-26 05:05:46

相關問題