I have 4 different ActionResult running in 2 different Controllers, I have created a partial view StudentList, I want to use same partialView as the model is same.
from 1st ActionResult I want to display StudentList by Class
from 2nd ActionResult I want to display StudentList by Class Teacher
from 3rd ActionResult I want to display StudentList by Fee Not Paid
from 4th ActionResult I want to display StudentList by Absent Student
all 4 return a Model type of Student.
with fields StudentName, ParentMobileNo
Is it possible not to create 4 different View and use single partial view or single view to display the result.
Regards
Yes. Create a shared view and pass the view name when returning the ActionResult from the controller.
return View("StudentList", model);
Or if you want to render a partial from a view:
#{ Html.RenderPartial("StudentList", model); }
Create a Partial View that will be hooked up to use your Student model.
Then create multiple actions in your controller for returning the different results.
StudentsByClass - then within this action call the relevant business layer/repository to do the query, as long as it's returning type Student (or the name of your model being used in your Partial View) it will be fine.
Then create another three actions for each of the scenarios, again calling the relevant business/repository method to do the query. Again as long as they return the same model that the Partial Student View is expecting it should work.
Then in each of the actions return the View along with the results to pass to the model like so:
return View("StudentList", model);
View should just worry about displaying model passed to it. How the model is created should be transparent to the view.
In your case,you can have a single view which just displays StudentList model passed to it. For generating this model you can have either one action method or four of them.
If you want single action method,you can pass a parameter indicating grouping.
Thanks
Prasad
Related
Need help. I want count players from clubs and show how much players are playing in club. This is my View
But i want that my code show player count in club, like this
Here is my database structure :
My starting code
public int GetCountOfPlayersInClub(int clubId)
{
using (var db = new BasketDbContext())
{
return db.Player.Count(p => p.BasketBallClubId == clubId);
}
}
But what I do now, what I need write in my ActionResult Index()?
In order to display value in view, you need to pass it over model or viewbag.
In view you can call the passed variable via razor syntax (like #Viebag.CountAll).
Note that count is not very optimized in ef core and I would recommend to ran raw query for it.in testing I did , raw query is 100 times faster.
I'm trying to show the results from a stored procedure on a view. I have a ASP.NET MVC application with the following code. I used EntityFramework to generate the models.
public class ProjectsController : Controller
{
private DatabaseEntities db = new DatabaseEntities();
// GET: Projects
public ActionResult Index()
{
var projects = db.Projects.Include(p => p.Headquarter);
return View(projects.ToList(), db.CALCULATEBUDGET());
}
}
I get the following errors on this part: db.CALCULATEBUDGET()
Argument 1: cannot convert from 'System.Collections.Generic.List<TestApplication.Models.Project>' to 'string' TestApplication C:\TestApplication\TestApplication\Controllers\ProjectsController.cs 21 Active
Argument 2: cannot convert from 'System.Data.Entity.Core.Objects.ObjectResult<TestApplication.Models.CALCULATEBUDGET_Result>' to 'string' TestApplication C:\TestApplication\TestApplication\Controllers\ProjectsController.cs 21 Active
My stored procedure:
CREATE PROCEDURE dbo.CALCULATEBUDGET
AS
SELECT MonthlyRent, Budget, 100 * H.MonthlyRent/P.Budget AS RentPercentage
FROM Headquarter H, Project P
WHERE H.HeadquarterId = P.Headquarter_HeadquarterId
I'm trying to calculate how much percent the rent is from the budget. And then I want to show the results in a view.
first of all, when you return a View you need to either pass the name or leave it empty and then it works out using the defaults. You can pass a model as well, but what you've done is to pass two data items. The first one needs to be a View name:
return View( "YourViewName", yourDataModel);
The main concept of MVC is this : here is a View and here is the data for a View in the form of a model.
Directly throwing objects from a database at a view is usually a bad idea, I would suggest you decouple your things a little bit.
Have a business layer where you get the data, you map into a model object which matches what the view needs to display. Think of that as a translation layer from what your data looks like and what your View needs to display. Rule of thumb, only send to a View whatever it needs to display and nothing more.
You can combine multiple multiple data items in one data model for the View if that's what you need, but you still pass just one object to your View.
and totally confused now .
i am using Database First approach for developing an application
my code have models generated by ER from Table/DB
and i have created modelview for each respective models.
i have to show some "applications" name in Dropdownlist via DB
and user have to insert respective "environment" name using a textbox and dropdown, auto populated on UI .
i have 2 viewmodel class "app_details and env_details:.
app_details have public properties
appID(identity), Appname
and env_details have
appID (fk), Envid(identity),envname.
i have done with adding the application name and have a SP to getappname and id
i am confused where to add
public IEnumerable Appdetail { get; set; }
without getting model state not valid
i want to bind my model to view. how to approach PLS
Simply You can create a Select list with the appropriate item key and value and Bind that with drop down by passing to helper.
I have to store data in the models as well as display from different model on the View in Webgrid.
I want to use #model projectname.models.modelname for storing the data in my modelname
and I also want to display data from another model(s) in Webgrid in the same view so i will have to use #model IEnumerable<projectname.Models.newmodel>
How can I use multiple models in same view and that too of IEnumerable..?
Is there any option which I can use..
I am new to MVC so please anyone can help me out..
You can create another class file like common.cs and in that you have to create public class and create members like
Public Class Employee
Public EmpNo As Integer
Public Designation As String
Public Address As String
Public Date_Of_Birth As DateTime
End Class
and you can access it by implement that common class file in the controller and use it by creating the object of the Employee class like
Employee e
e.EmpNo
e.Designation
e.Address
e.Date_Of_Birth
and set those values.
Now you have to use it in view so you can access these values directly in view:
#Models.Common.UserName
Here i declared Models because my common file is in Models that's why.
I am using SimpleMembership in an MVC4 web app. I can't figure out how to edit the profile information. I thought I could do it just as you do any other table.
[HttpPost]
public ActionResult EditUser(UserProfile user)
{
if (ModelState.IsValid)
{
udb.Entry(user).State = EntityState.Modified;
udb.SaveChanges();
return RedirectToAction("Index");
}
But I get an error saying entity state does not exist in the current context. My context is defined as follows at the top of the controller.
private UsersContext udb = new UsersContext();
I can find plenty of references on access profile data but nothing for editing the data. How can I save the edited UserProfile data back to the db?
EDIT: I was able to resolve the entityState error -- I had to include system.data and system.data.entity. However now when I run I get an error on edit which says unexpected number of rows modified (0). and points to the udb.SaveChanges() line. Still can't figure out how to modify UserProfile data elements.
Simple answer. I needed to set all the fields for the Model in my view. I was only allowing the user to change 4 out of the 6 so two were not being set.
I thought when you pass the model to the view, the view would pass the same field values onto the action if they were not set in the view. For example: if I set FirstName in the view but not UserName the original UserName that was sent to the view would be passed to the Model. That appears not to be the case. For all the items in the model I did not allow them to change in the view I had to set hidden fields to set the fields so a complete model was sent.
It may be better to set individual fields but I am not sure how to do that yet and that was not the question.