Partial control of ASP.NET MVC

I am new to mvc. I have worked a bit with Winforms and am now working on an MVC project. In our winforms project, we have standard controls, such as a drop-down list that will show all employees. This control knows how to get employees and display them. It provides a property that gives the selected employee. Forms just have to put control on it and use it.

Now my question is how can I achieve this in aps.net MVC (in views). I tried partial views, but then how can I put the selected value into the parent view

0
source share
3 answers

In MVC, Views should not contain any logic other than display logic.

Controller must be one that "knows how to get employees" or create a Model employee, which then goes to View , which knows how to display the employee, in most cases it is a strongly typed View type Employee .

+1
source

I think this is what you are trying to achieve:

Say that in your model you have an Employee (or a list, either):

 Employee Employee { get; set; } 

So, your main view should have the following:

 @Html.DisplayFor(Model => Model.Employee); 

But how can I create a template, you ask?

You should take a step back at this point and consider creating a new ViewModel, EmployeeViewModel (this, in your opinion, should be your "partial view").

So now you will have:

 EmployeeViewModel Employee { get; set; } 

Now create a folder in your views / general calls to DisplayTemplates and create EmployeeViewModel.cshtml. To be clear, you now have / Views/Shared/DisplayTemplates/EmployeeViewModel.cshtml .

The top line of this should be read:

 @model YourNamespace.EmployeeViewModel 

And now you can just use in this view:

 @Html.LabelFor(model => model.EmployeeName) // or whatever properties you have 

That should be enough to get you started.

+1
source

PartialView will be good for you. so find some content with the keyword "PartialView"

0
source

All Articles