Send data from partial view to controller and vice versa - ASP.NET MVC 5

Hi All,

This is my first MVC application. I have a view(SupportCaseSystem.cshtml) which has 3 parts.

1. Employee Details - Search Textbox, Name,Email, Department,Phone(When employeeid is entered in textsearchbox the details should be populated via AJAX)
2. Manager Details - Same as above
3. FindSupportCases Button and Grid. - When Clicked on the button supportcases currently allocated to the selected employee id should be retrieved and displayed in grid using Ajax.

I created two viewmodels and two partial views.
public class GetEmployeeDetails
public  string empid {get;set;}
public  string Name {get;set;}
public  string Email {get;set;}
public  string Department {get;set;}
public  string Phone {get;set;}

public class GetSupportCasesViewModel
public  string empid {get;set;}
public int casenumber {get;set;}
public DateTimeSubmitted {get;set;}
public string casedetails {get;set;}
@model EMS.GetEmployeeDetailsViewModel

@model EMS.GetSupportCasesViewModel
--populate webgrid here

public ActionResult  GetEmployeeDetailsById(string empid)
 var EmplVM = new GetEmployeeDetailsViewModel()
EmployeeFacade Efac = new EmployeeFacade();
Employee emp = Efac.GetInfo(empId);
EmplVM.Name = emp.Name;
return view(EmplVM);

Similar way written for get support cases.

Open in new window

Below are my questions.

1. In my main view file SupportCaseSystem.cshtml how do i embed these partial views? The first partial view needs to be used twice once for employee and once for manager. The second partial view is grid to show tickets.

2. When view loads initially only the first partial view for employee and manager should be visible with their values as blank. After user enters emp id i need to fill the details by Ajax call.  Same for manager details.

Now how do i send the empid from partial view to my controller ? 1. by using Ajax and 2. by not using Ajax.
2. How do i bind the returned data to the controls in partial view ?

Hope i am clear. Thanks for your time and any help is appreciated.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gautham JanardhanCommented:
ideally u rener partial view like shown below

@{ Html.RenderPartial("someview.cshtml", passtheModelHere);}

Open in new window

This is if you are not going to laod data using ajax.

If you are using ajax then pass an empty model to the view so tht u can render the view and then set the control values on ajax  call back


Use .load () method of jquery to render the view directly by passing the employee id (here the loading of data should be handled inside the partial view).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JyozealAuthor Commented:
thanks for your comment. Where should i provide the Ajax code? I added it in partial view but its not getting called. Only if its in partial view i can use it somewhere else too.

I am trying to create a partial view which consists of once search box search button and table with details. Whenever an employee id is entered and search clicked it goes to my controller and get the details back.

When i tried this javascript written inside partial view doesn't seem to work. Any idea about this? Also if you could post some code..
Gautham JanardhanCommented:
how are u adding in the partial? It shud get called even if its in the partial code
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

JyozealAuthor Commented:
thanks for your comment. I included script tag in the partial view like below.

<script type="text/javascript">

jquery ajax call here

JyozealAuthor Commented:
there is no error in my ajax code. Its working from main view.
Gautham JanardhanCommented:
can you post some of your code in the partial view.
JyozealAuthor Commented:
That answer helped.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.