Kajah User
Kajah User

Reputation: 591

How to get the particular dictionary values in asp.net mvc

So far i am using this coding in the mvc 2 in controller page. Here employeeInfoList = EmployeeInfoProxy.GetAllEmployeeInfo(TenantId); employeeInfoList in that i geting the values of the particular 'tenantid' after that i wnat to assign it in the viewdata for the particular view...

so that is used valus.tolist() i am getting generic error.......

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

Upvotes: 0

Views: 930

Answers (3)

Kajah User
Kajah User

Reputation: 591

<%@ 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>
<% } %>

Upvotes: 0

Allan Nienhuis
Allan Nienhuis

Reputation: 4111

EmployeeInfo employee = employeeInfoList.Values.ToList();

the ToList() method returns a List object, but you are trying to assign it to a single employeeInfo object.

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

Upvotes: 0

indiPy
indiPy

Reputation: 8062

You are casting list to one obj, it should be same type. If it is returning dictionary or any list type then you should iterate with it or get the DefaultOrSingle() in that list. See if this works,

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

Upvotes: 2

Related Questions