System.Data.Objects.ObjectSet`1 instead of database table - asp.net-mvc-3

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();

Related

Passing data from Controller to model in MVC 3

is there a way to pass data from controller to model?
the scenario of my project is that, it has two tables in database, and in View there are two text boxes , the data of one text box is save to one table while data of another table is save to another table , i want to show the data of both tables in another single View . for that reason i want to send both textbox values from controller to model then want to show the data from that model to the view.
can someone please help how can i implement it ?
You need to create a view model and use that. view model's are simple POCO classes which are specific to the view.
public class CustomerInfo
{
public string Name {set;get;}
public string AddressLine {set;get;}
}
And in your GET Action, you can create an object of this, read the data from 2 tables and set the property values of our view model and send to the view
public ActionResult View(int id)
{
var vm = new CustomerInfo();
var db=new YourDbContext();
var customer=db.GetCustomer(id); // read from first table
var address=db.GetAddress(id); // read from second table
vm.Name = customer.FirstName;
vm.AdddressLine = address.AddressLine1;
return View(vm);
}
And your view will be strongly typed to this view model
#model CustomerInfo
<h2>#Model.Name</h2>
<h3>#Model.AddressLine1</h3>
If you are trying to save this information to the db tables, You may have a form in your view
#model CustomerInfo
#using(Html.BeginForm("Edit","Home"))
{
<label>Name</label> #Html.TextBoxFor(f=>f.Name)
<label>Name</label> #Html.TextBoxFor(f=>f.AddressLine)
<input type="submit" />
}
and in your HttpPost action
[HttpPost]
public ActionResult Edit(CustomerInfo model)
{
// read the values from model and save to 2 tables
var c=new Customer { FirstName=model.Name};
var a=new Address{ AddressLine1=model.AddressLine};
var db=new YourDbContext();
db.Customers.Add(c);
db.Addresses.Add(a);
db.SaveChanges();
return RedirecToAction("Success");
}
If you want to save a collection of items, You may refer this answer or this.

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.

How to show custom query result to view mvc3 Razor

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.

How can I get info from database?

I have a database CarsDB with a table Car with some columns (like mileage or model)
And I want to get info from that table in a controller I write:
public ActionResult Index()
{
return View(db.CarsDB.ToList()):
}
But I'm getting an error (something like method CarsDB not found). The error is long and I don't know hot to translate it to English :( Also, when I write db, the only suggetions I'm getting are "Equals", "GetHashCode" and some others, not with CarsDB
Try
db.Cars.ToList()
You need to query the table (cars) - right now you're saying "select everything from the CarsDB database", rather than "select everything from the Cars table in the CarsDB database".
To clarify - your db variable should be an instance of a data context or entities.
Full code should look something like this:
public ActionResult Index()
{
CarsDB db = new CarsDB();
return View(db.Cars.ToList()):
}
If you're using Entity Framework you could do something like:
public ActionResult Index()
{
using (CarsDB dataContext = new CarsDB())
{
return View(dataContext.Cars.ToList()):
}
}
This will also assure automatic disposal of your data context.

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>();

Resources