How to show custom query result to view mvc3 Razor - asp.net-mvc-3

I have Custom query Result as
public ActionResult Index()
{
var Query = (from E in db.SITE_MASTER.AsEnumerable()
where E.IS_PAGE == true
select new
{
E.POST_TITLE,
E.POST_TEXT
}).ToList();
return View(Query);
}
Now,
How can I make view for this result or How can I create View for this Result Query.
Thanks...

You can pass an anonymous type as a model, but please don't. It's ugly and will lead to maintenance problems.
As an alternative either use the ViewBag or create concrete type and pass that.

Related

how to show data in view from Viewbag mvc4?

this is my controller
public ActionResult Index()
{
var data = (from o in db.aspnet_Users
from q in db.aspnet_Profile
where
o.UserId == q .UserId
select new{
o.UserId,
o.UserName,
q.Address,
q.Email,
q.Phone,
q.Mobile,
q.SocialID,
}
).ToList();
ViewData["Allprofile"] = data;
return View();
}
and i want to show the data in the view in table, there was a problem to show 2 db Models in one view because the model generated from Database not Code First so i'm trying show the data using ViewData any help ?
why are you not using a ViewModel here? you are somewhat defeating the purpose of MVC.
the view model will encapsulate the data you need to show on the View. you pass the view model to the View - the view will accept the strongly typed view.
the ViewData should never really be used for heavy/complex objects for a number of reasons - performance for one, but also no type safety/strongly typed support either.
change your code so you return a ViewModel with the data you need to show to the user.

System.Data.Objects.ObjectSet`1 instead of database table

I have database CarsDB, with Table Car in it. I whant to see the car table, but getting only System.Data.Objects.ObjectSet`1 instead of database table
public CarsBDEntities db = new CarsBDEntities();
public ActionResult Index()
{
ViewBag.Carlist = db.Cars.ToString();
return View();
}
I have make a table, then created a model from it. Using entity frameworkd and ado.net.
You don't want to use ToString; if you just return db.Cars instead, that will give you an IEnumerable that you can work with.
var cars = db.Cars;
return View(cars);
You can use this to make a Webgrid that will display your data. Razor view:
#model IEnumerable<Namespace.Models.Cars>
#{
var grid = new WebGrid(Model);
}
#grid.GetHtml()
You're trying to convert an ObjectSet to a string, which is why your getting "System.Data.Objects.ObjectSet". You'll need to pull the data out of the table to view it. For instance, something like:
string firstCarMake = db.Cars.FirstOrDefault().Make.ToString();

Returning a list to view and manipulating it in MVC3, Database first approach

I have a function that triggers a stored procedure that runs a
select * from tbl_admin where id=".." and username=".."
The returned output of the select statement is stored in a complex return type object spa_user_Result and stored onto a list.
I want to pass this list to the view and use it to display data, How do I do that??
code is smthing like this:
public ViewResult Index()
{
<spa_users_Result> result = new <spa_users_Result>();
System.Data.Objects.ObjectResult<spa_users_Result> r;
r = db.adminUser("s","superuser");
result = (<spa_users_Result>)r.ToList();
return View();
}
You need to pass your object(s) to the View method
public ActionResult YourAction()
{
var result = db.adminUser("s","superuser").ToList();
return View(result );
}
Assuming your db.adminUser("s","superuser") method returns a Collection of spa_users_Result object.
Make your View strongly typed to List of spa_users_Result object
#model IEnumerable<spa_users_Result>
#foreach(var item in Model)
{
<p>#item.Name</p>
}
Assuming your spa_users_Result class has a Property called Name
I'm not entirely sure what level of detail you're looking for with this question, but passing the list to the view is as simple as:
return View(result);
EDIT: Also don't you want result to be a list?
List<spa_users_Result> result = new List<spa_users_Result>();

Return a view from Post method

I have this post method:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Invitations(SuperInvitationsEditModel model)
{
...
var newmodel = new SuperInvitationsEditModel();
if (hasErrors)
{
SuperInvitationsErrorModel newErrorModel = new SuperInvitationsErrorModel();
newErrorModel.Errors = model.Errors;
return View(newErrorModel);
}
return View(newmodel);
}
When this code in the if(hasErrors) executes I get this error.
The model item passed into the dictionary is of type 'MyProject.Models.SuperInvitationsErrorModel', but this dictionary requires a model item of type 'MyProject.Models.SuperInvitationsEditModel'.
I thought I can do this since the return value of the method is a generic ActionResult. Can anyone tell me why is this not working?
because your current view is strongly typed. change the code as
return View("yourviewname",newErrorModel);
It has nothing to do with casting ViewResult to ActionResult. The problem is, that you have strongly typed view that expects the model of type SuperInvitationsEditModel (see #model on the top of Invitations.cshtml), but you are passing the model of type SuperInvitationsErrorModel to it.
You should merge the two view model classes (SuperInvitationsEditModel and SuperInvitationsErrorModel) into one, or create a standalone view for each of them.

MVC 3 Details View

I am new to MVC frame work. And i am making one page where we can see details of department by clicking on details link button.
While User click link button it fetch the all the records of the particular department in List Collection and redirect to Details View.Data has been fetched in List but while going to Details view it Generates following error:
The model item passed into the dictionary is of type 'System.Collections.Generic.List1[DocPageSys.Models.Interfaces.DepartmentInfo]', but this dictionary requires a model item of type 'DocPageSys.Models.Interfaces.DepartmentInfo`'.
I understood the error but confusion to solve it.And stuck with this problem...
Since your Details view is strongly typed to DepartmentInfo:
#model DocPageSys.Models.Interfaces.DepartmentInfo
you need to pass a single instance of it from the controller action instead of a list:
public ActionResult Details(int id)
{
DepartmentInfo depInfo = db.Departments.FirstOrDefault(x => x.Id == id);
return View(depInfo);
}
So make sure that when you are calling the return View() method from your controller action you are passing a single DepartmentInfo instance that you have fetched from your data store.
To make it run fine initially you could simply hardcode some value in it:
public ActionResult Details(int id)
{
var depInfo = new DepartmentInfo
{
Id = 1,
Name = "Sales",
Manager = "John Smith"
}
return View(depInfo);
}
Oh, and you will notice that I didn't use any ViewData/ViewBag. You don't need it. Due to their weakly typed nature it makes things look really ugly. I would recommend you to always use view models.
Passing a list instead of a single item
This error tells you, that you're passing a list to your view but should be passing a single entity object instance.
If you did fetch a single item but is in a list you can easily just do:
return View(result[0]);
or a more robust code:
if (result != null && result.Count == 1)
{
return View(result[0]);
}
return RedirectToAction("Error", "Home");
This error will typically occur when there is a mismatch between the data that the controller action passes to the view and the type of data the view is expecting.
In this instance it looks as if you're passing a list of DepartmentInfo items when your view is expecting a single item.

Resources