2010-11-27 2 views
0

До сих пор я использую эту кодировку в mvc 2 на странице контроллера. Здесь employeeInfoList = EmployeeInfoProxy.GetAllEmployeeInfo(TenantId); employeeInfoList в том, что я адресности значения конкретного «tenantid» после этого я wnat назначить его в ViewData для конкретной точки зрения ...Как получить значения словаря в asp.net mvc

так, что используется valus.tolist() я получаю общая ошибка .......

public ActionResult Edit(string TenantId) 
{ 
    try 
    { 
     Dictionary<string, EmployeeInfo> employeeInfoList = new Dictionary<string, EmployeeInfo>(); 
    employeeInfoList = EmployeeInfoProxy.GetAllEmployeeInfo(TenantId); 
    if (employeeInfoList != null) 
    { 
     EmployeeInfo employee = employeeInfoList.Values.ToList(); //here i want to bind the values to the employeeinfo but genric error is cuming 
     ViewData["Department"] = employee.Department; 
     ViewData["Designation"] = employee.Designation; 
     ViewData["Address1"] = employee.Address1; 
    } 

    } 
    catch (Exception ex) 
    { 
    } 
    return View(); 
} 
+0

Какую ошибку вы видите? – TJB 2010-11-27 06:15:53

+0

может уяснить это дальше .. Что возвращает GetAllEmployeeInfo (TenantId). И какая ошибка у get ..? – Novice 2010-11-27 06:17:21

+0

EmployeeInfoProxy.GetAllEmployeeInfo (TenantId); – 2010-11-27 06:49:57

ответ

0
EmployeeInfo employee = employeeInfoList.Values.ToList(); 

метод ToList() возвращает объект List, но вы пытаетесь присвоить его к одному объекту employeeInfo.

Dictionary<string, EmployeeInfo> employeeInfoList = new Dictionary<string, EmployeeInfo>(); 
    employeeInfoList = EmployeeInfoProxy.GetAllEmployeeInfo(TenantId); 

    if (employeeInfoList != null) 
    { // assuming you really only want the first one. What if GetAllEmployeeInfo() returns more than one employee??? 
     EmployeeInfo employee = employeeInfoList.Values.FirstOrDefault(); 
     ViewData["Department"] = employee.Department; 
     ViewData["Designation"] = employee.Designation; 
     ViewData["Address1"] = employee.Address1; 
    } 
2

Вы листинг списка в один объект, он должен быть того же типа. Если он возвращает словарь или какой-либо тип списка, вы должны перебирать его или получить DefaultOrSingle() в этом списке. Смотрите, если это работает,

var empinfo =EmployeeInfoProxy.GetAllEmployeeInfo(TenantId).FisrstorDefault(); 
if (empinfo !=null) 
{ 
    EmployeeInfo employee = employeeInfoList.FisrstorDefault(); 
    ViewData["Department"] = employee.Department; 
    ViewData["Designation"] = employee.Designation; 
    ViewData["Address1"] = employee.Address1; 
} 
0
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="CelloSaaS.View.CelloViewPage" %> 


<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"> 
<% using (Html.BeginForm("Edit", "EmployeeInfo")) 
    {%> 
<div class="content-admin"> 
    <div class="page-header"> 
     <h1> 
      <asp:Literal ID="addPackage" runat="server" Text="Employee" /></h1> 
     <div class="imageAlign"> 
      <input type="image" src="<%= this.ResolveClientUrl("../../App_Themes/CelloSkin/btn-save.gif")%>" 
       alt="Add Employee Details" /> 
      <a href="..EmployeeInfo/EmployeeInfoList"> 
       <img src="<%= this.ResolveClientUrl("../../App_Themes/CelloSkin/ico-previous.png")%>" 
        alt="Back To Employee List" /> 
      </a> 
     </div> 
    </div> 
    <table border="0" cellspacing="0" cellpadding="0"> 
     <tr> 
      <td> 
       <table cellpadding="0" cellspacing="0" border="0" class="table-main"> 
        <tr> 
         <td> 
          <table width="100%" cellpadding="0" cellspacing="1" class="details"> 
           <thead> 
            <tr> 
             <th colspan="4"> 
              <asp:Literal ID="litPackageDetails" runat="server" Text="EmployeeDetails" /> 
             </th> 
            </tr> 
           </thead> 
           <tr> 
            <td class="row4"> 
             <asp:Literal ID="Literal1" runat="server" Text="EmployeeName"> 
             </asp:Literal> 
            </td> 
            <td class="row5"> 
             <%= Html.TextBox("Name", (string)ViewData["Name"])%> 
            </td> 
            <td class="row4"> 
             <asp:Literal ID="DepartmentName" runat="server" Text="DepartmentName"> 
             </asp:Literal> 
            </td> 
            <td class="row5"> 
             <%= Html.TextBox("Department", (string)ViewData["Department"])%> 
            </td> 
           </tr> 
           <tr> 
            <td class="row4"> 
             <asp:Literal ID="Literal2" runat="server" Text="Designation"> 
             </asp:Literal> 
            </td> 
            <td class="row5"> 
             <%= Html.TextBox("Designation", (string)ViewData["Designation"])%> 
            </td> 
            <td class="row4"> 
             <asp:Literal ID="Literal3" runat="server" Text="Salary"> 
             </asp:Literal> 
            </td> 
            <td class="row5"> 
             <%= Html.TextBox("Salary", (string)ViewData["Salary"])%> 
            </td> 
           </tr> 
           <tr> 
            <td class="row4"> 
             <asp:Literal ID="Address1" runat="server" Text="Address1"></asp:Literal> 
            </td> 
            <td class="row5"> 
             <%= Html.TextArea("Address", (string)ViewData["Address"])%> 
            </td> 
            <td class="row4"> 
             <asp:Literal ID="Literal4" runat="server" Text="Address2"></asp:Literal> 
            </td> 
            <td class="row5"> 
             <%= Html.TextArea("Address1", (string)ViewData["Address1"])%> 
            </td> 
           </tr> 
          </table> 
         </td> 
        </tr> 
       </table> 
      </td> 
     </tr> 
    </table> 
</div> 
<% } %>