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.
Related
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.
I have a situation where i have to take input(form) from user. After continue button is pressed next view page is displayed. But after continue is pressed i don't want to store the model in the DB. I have to display some details(combining some tables) according to input given by the user earlier and again get some data from user. Only then i want to store the model in the respective tables.
How can i perform this? I tried getting Model from user and passing to the function that generates next page. Is this is way to do it? or there is other way around?
Store the model submitted by the first form in session.
[HttpPost]
public ActionResult ContinueForm1(Model1 model1)
{
if(ModelState.IsValid)
{
Session["Model1"] = model1;
return View("Form2");
}
return View();
}
[HttpPost]
public ActionResult ContinueForm2(Model2 model2)
{
if(ModelState.IsValid)
{
... model2 is already here, get the model1 from session
... and save to datatbase finally return a different view or redirect to some
... other action
}
return View();
}
You are heading down the right track.
You need to grab the model that is passed back from the first view - preferably you are using ViewModels here rather than binding directly to your db models. Have a look at http://lostechies.com/jimmybogard/2009/06/30/how-we-do-mvc-view-models/ and Why should I use view models? as to why these are good things.
The easiest way to do this is to pass the model in as an argument to your method e.g.
Assuming that your views are using the same ViewModel ( which may or may not be true) then you can send the viewmodel straight to your new view - else you can copy the elements into a new viewModel and send that.
e.g.
[HttpPost]
public ViewResult Step1(MyViewModel viewModel)
{
//Do some validation here perhaps
MySecondViewModel secondViewModel = new MySecondViewModel{
Id = viewModel.Id,
// etc. etc.
};
return View("Step2", secondViewModel);
}
Then you can carry on as you need until you have to persist the entity to the database.
NB as you do not need to do anything special in the form to make it post the model as an argument as long as the view is strongly typed to that ViewModel.
My first MVC3 EF 4.2 site and I'm confused on some things, currently on ViewModels when querying and saving. Please correct me if I explain this poorly, i'm not sure how to term this. The .edmx automatically created the table classes but I read it was better to create a ViewModel, considering I need to join tables to display/edit my Product completely. The controller code below is where I join tables to output a Product to edit, and then save. My question - what is the right way to save the Product, to the Product.cs model generated by DbContext or my own ProductViewModel.cs?
Is there an easier method to query a product and join the tables and then map to the viewmodels parameters, or do I keep doing all this in the controller like below?
I also want to save/update the product each time someone views/clicks on the product, so I wasn't sure if I create a separate ViewModel for updating just that parameter or again, use the Product model.
Hope that makes sense! I can explain further if needed.
private SiteForgeEntities db = new SiteForgeEntities();
public ActionResult Edit(int id)
{
var viewModel = (
from a in db.Products
join b in db.Sites
on a.SiteId equals b.SiteId
join c in db.Sections
on a.SectionId equals c.SectionId
join d in db.Affiliates
on a.AffiliateId equals d.AffiliateId
select new ProductViewModel()
{
ProductId = a.ProductId,
Product = a.Product,
Description = a.Description,
Image = a.Image,
Price = a.Price,
Clicks = a.Clicks,
Link = a.Link,
Site = b.Site,
Section = c.Section,
Affiliate = d.Affiliate
}).Single(x => x.ProductId == id);
return View(viewModel);
}
[HttpPost]
public ActionResult Edit(Product product)
{
...update database...do I pass in and save back to Product or my ProductViewModel
}
You use ViewModel to pass multiple models to the view, but when you save data, you need to save it to the appropriate model. If you are adding or modifying products, you will add items to products (using your DbContext). If you have one-to-many relationship defined between two models (in your Product.cs model you might have a property declared as:
public virtual ICollection<SomeOtherModel> SomeOtherData { get; set; }
you can use this to build a table instead of passing everything in a ViewModel. There is a nice tutorial here regarding the CRUD operations using EF4. Have a look at these short tutorials that can give you an idea about your strategy http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc.
I am just new to MVC.
when we use "#Html.EditorFor" in razor view, it generates textbox.
My requirement is that I need to supply some value from viewbag or session to user's in that textbox?
Is it possible and if yes how can i do?
OR
What are the alternatives?
In your action method in the controller, pre-load a model with some data:
public ActionResult Index()
{
MyModel model = new MyModel();
model.FirstName = "Bob";
model.LastName = "Hoskins";
return View(model);
}
Then make your View strongly typed. These pre-set values should now appear on your view. You probably want to populate them from a service layer or resource file, rather than have them as hardcoded strings like my example.
I need to create a linq statement that combines 2 tables and send that to the view.
Do I need to create a ViewModel for this.
Say the output will be the following
Table1.Vendor Table1.VendorName Table2.Address
It depends what your result type is.
If you return a dynamic object an anonymous type then you can't put that into the ViewBag directly (see: Stuffing an anonymous type in ViewBag causing model binder issues). But if you're returning something that's strongly typed, you can just put that straight into the ViewBag and bypass having a model.
That said, I'd always lean towards having a strongly typed model!
Yes You Should use View-model because if u want to send more than one table you may
use ViewBag or something equivalent viewdata[] that's wrong etc..
Because ViewBag for example is dynamic type it can't be sent to view with 2 table
For example if you do something like this
ViewBag.Workers=db.Workers.Join(idb.AspNetUsers, e => e.UserID, i => i.Id, (e, i) => new { UserName = i.UserName, SBIN = e.SBIN, }).ToList();
You will Get error 500 when you use this viewbag in your View
<span>#ViewBag.Workers.Anything</span>
Even in this example you should use for-each you still get error becausethe ViewBag is internal
So You could use something like that in the controller for this issue
var Workers=db.Workers.Join(idb.AspNetUsers, e => e.UserID, i => i.Id, (e, i) => new WorkerViewCardModelcs{ UserName = i.UserName, SBIN = e.SBIN, }).ToList();
return PartialView("_WorkerCard", Workers);
and Don't for get to use #model in the View or Partial View
#model IEnumerable<FinalProject.ViewModels.WorkerViewCardModelcs>
#foreach (var item in Model)
{
What ever logic you wish
}