Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MVC3
Hii to all,
I have a requirement that i need to get complete rows data of a table from view to controller and then display in another different view, The actual scenario is i have a table where i need to display the employees data like Empid,Ename,Task,in addition to this columns i have placed one more field called Edit(Link Field),so that with the help of Edit link field we can update employee details, here what i need is whenever i clicked on EditLink i need to redirect to another page say editable page,in this editable page i need to get the respective rows(the row editlink is fired)data and dispaly in the textbox's in an editable mode, so that he can modify the employee details and click on submit buttton then changes should reflect to the table. Here i iam not using any database for displaying table data, in the view itself i designed the table & populated the data to the table fields,please guide me how can i pass complete rows data from one View to another different view..thanks in advance
 
Thanks
Ramu
Posted 1-May-13 4:15am
Comments
Jameel Moideen at 1-May-13 10:44am
   
did you try anything?
Jameel Moideen at 1-May-13 10:45am
   
Please post the code of listed table view and controller.
Ram7 from Hyderabad at 2-May-13 0:37am
   
Dear Moideen,Thanks for ur intrest towards my post,here is my controller code
public ActionResult Login(UsersLogin model)
{
if (model.UserName == "employee1" && model.Password == "ramu")
{
//return View("~/Views/Account/Register");
return View("employee1");
}
else if (model.UserName == "employee2" && model.Password == "ramu2")
{
return View("employee2");
}
else if (model.UserName == "manager" && model.Password == "manager")
{
return View("manager");
}
else if (model.UserName == "admin" && model.Password == "admin")
{
return View("admin");
}
 
else
{
ViewData["Error"] = "Invalid UserId or Password";
return View();
}

}
I Have created 5 views
1)Normal Login View:-

Member Login

type="text/javascript"></script>
@Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.")
@using (Html.BeginForm()) {
<div>
<fieldset>
@* Account Information
*@
<div class="editor-label">
@Html.LabelFor(m => m.UserName)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</div>
 
<div class="editor-label">
@Html.LabelFor(m => m.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</div>
 
@*<div class="editor-label">
@Html.CheckBoxFor(m => m.RememberMe)
@Html.Label(ViewData["Error"])
</div>*@
<div>
@ViewData["Error"]
</div>
<p>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
}
2)employee1 View:-
<table>
<tr>
<th></th>
<th>Empid</th>
<th>Ename</th>
<th>Task</th>
</tr>
<tr>
<td>@Html.ActionLink("Edit","Edit")
</td>
<td>101</td>
<td>Ramu</td>
<td>Fixing Bugs</td>
</tr>
</table>
3)Similarly employee2 view:-

You are Logged in as employee2

 
<table>
<tr>
<th></th>
<th>Empid</th>
<th>Ename</th>
<th>Task</th>
</tr>
<tr>
<td>@Html.ActionLink("Edit","Edit")</td>
<td>102</td>
<td>Vijay</td>
<td>Testing Code</td>
</tr>
</table>
4)Manager view:-

You are Logged in as Manager

 
<table>
<tr>
<th></th>
<th>Empid</th>
<th>Ename</th>
<th>Task</th>
</tr>
<tr>
<td>@Html.ActionLink("Edit","Edit")
<td>101</td>
<td>Ramu</td>
<td>Fixing Bugs</td>
</tr>
<tr>
<td>102</td>
<td>Vijay</td>
<td>Testing Code</td>
</tr>
</table>
5)Admin view:-
<table>
<tr>
<th.</th>
<th>Employees</th>
<th>ManagerName</th>
<th>ManagerTask</th>
</tr>
<tr>
<td>@Html.ActionLink("Edit","Edit")
<td>Ramu</td>
<td>RamaKrishna</td>
<td>Code Review</td>
</tr>
<tr>
<td>Vijay</td>
<td>Naresh</td>
<td>Code Review</td>
</tr>
</table>
Hear i need to create one
Jameel Moideen at 2-May-13 11:48am
   
Hi friend..i have posted a small example whatever you expect for u ..please follow this and change the business logic that you want..Hope this helps

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You don't need to create a separate views for this. I have create a sample application whatever you expect. Please follow the steps
Step1 : Create a model under the Model folder like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication1.Models
{
    public class MyDataViewModel
    {
        public string Selection { get; set; }
        public List<Manager> ManagersData{get;set;}
        public List<EmployeeOne> EmployeeOneData { get; set; }
        public List<EmployeeTwo> EmployeeTwoData { get; set; }
    }
    public class Manager 
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
     public class EmployeeOne 
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
     public class EmployeeTwo 
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}
Step2: Create three Partial Views under shared folder for manager,Employee1,Employee2,etc like below
EmployeeOne Partial View
@model IEnumerable<mvcapplication1.models.employeeone>
@{
    ViewBag.Title = "_employeeOne";
}
<h2>_employeeOne</h2>
 
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th></th>
    </tr>
 
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}
</table></mvcapplication1.models.employeeone>
Manager Partial View
@model IEnumerable<mvcapplication1.models.manager>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th></th>
    </tr>
 
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}
</table></mvcapplication1.models.manager>
Create other partial views like which return the IEnumerable collection or whatever your collection format
Step3: Create Controller(SelectViewController) and ActionResult(Index) like below
public class SelectViewController : Controller
   {
       public ActionResult Index()
       {
//based on your login logic set the data to the collection property of MyDataViewModel class and return this model to the view
           var model = new MyDataViewModel();
           string yourLoginStatus = "EmployeeOne";
           if (yourLoginStatus == "Manager")
           {
               model.ManagersData = ManagerData();
//Set the selection status here
               model.Selection = "Manager";
           }
           else if (yourLoginStatus == "EmployeeOne")
           {
               model.EmployeeOneData = EmployeeOneData();
//Set the selection status here
               model.Selection = "EmployeeOne";
           }
           else if (yourLoginStatus == "EmployeeTwo")
           {
               model.EmployeeTwoData = EmployeeTwoData();
//Set the selection status here
               model.Selection = "EmployeeTwo";
           }
           return View(model);
       }
//this method loads the manager  data
       public List<Manager> ManagerData()
       {
           var manager = new List<Manager>();
           manager.Add(new Manager{Id=1,Name="Manager1"});
            manager.Add(new Manager{Id=2,Name="Manager2"});
           return manager;
       }
//this method loads the employee one data
       public List<EmployeeOne> EmployeeOneData()
       {
           var employee = new List<EmployeeOne>();
           employee.Add(new EmployeeOne { Id = 1, Name = "Emp1" });
           employee.Add(new EmployeeOne { Id = 2, Name = "Emp2" });
           return employee;
       }
//this method loads the employee two data
       public List<EmployeeTwo> EmployeeTwoData()
       {
           var employee = new List<EmployeeTwo>();
           employee.Add(new EmployeeTwo { Id = 1, Name = "EmpTwo1" });
           employee.Add(new EmployeeTwo { Id = 2, Name = "EmpTwo2" });
           return employee;
       }
   }
 
Step 4 :Finally Create a View by right clicking the Index action
 
@model MvcApplication1.Models.MyDataViewModel
@{
    ViewBag.Title = "Index";
}
//Based on the status that we are setting inside the index action load the partial views
@if (Model.Selection == "Manager")
{
     Html.RenderPartial("_manager", Model.ManagersData);
}
else if (Model.Selection == "EmployeeOne")
{
     Html.RenderPartial("_employeeOne", Model.EmployeeOneData);
}
else if (Model.Selection == "EmployeeTwo")
{
     Html.RenderPartial("_employeeTwo", Model.EmployeeTwoData);
}
  Permalink  
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 240
1 Kamal Rocks 184
2 PIEBALDconsult 150
3 BillWoodruff 148
4 Jochen Arndt 135
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 2 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100