2
public ActionResult Detay(int? categoryId)
{
    var categories = categoryService.CategoriesToList();
     if (categoryId == null)
     {
        var products = productService.Products().ToList();  

        return RedirectToAction("Index");
     }
     else
     {
        var products = productService
                      .CategoryProducts((int) categoryId, 50)
                      .ToList();
        var result = products.Where(a => a.CategoryId == categoryId);
        return View(result);
     }

}

I have Product Controller and this is my method for send products to view by category.

I want to check categoryId in my View

Like this;

@if(categoryId==1){//do this.}

But I cant reach categoryId how can I send that data and get data from view?

ekad
  • 14,436
  • 26
  • 44
  • 46
sercanD
  • 187
  • 1
  • 13
  • try ViewBag `ViewBag.category = "assign";` at controller and and in view `@if(@ViewBag.category=="1"){//do this.}` – Sachu May 18 '15 at 06:50
  • Try to pass object of product like Product prod=products.Where(a => a.CategoryId == categoryId); return view(prod); And in view you need to accept model as product – Mairaj Ahmad May 18 '15 at 06:51
  • You need to access a property of the model your returning (in your case the model is a collection so maybe `Model[0].CategoryId` or you could assign the value to a `ViewBag` property in the controller and use `ViewBag.CategoryId` –  May 18 '15 at 06:52

3 Answers3

4

According to your controllers code, your View receives IEnumerable<Product> as model and not a Product itself. Either create an new viewmodel and use it:

public class ProductsViewModel
{
   public int CategoryId {get;set;}
   public IEnumerable<Product> Products {get;set;}
}

and in your view:

@model ProductsViewModel

@if(Model.CategoryId==1)..

or in your view use @Model.First().CategoryId

Alex Art.
  • 8,711
  • 3
  • 29
  • 47
0

View Model:

public class ProductsViewModel
{  
   public int CategoryId{get;set;}
   public IEnumerable<Product> Products {get;set;}
}

Controller:

public ActionResult Detay(int? categoryId)
{
  var productVM= new ProductsViewModel();
  var products = productService
                      .CategoryProducts((int) categoryId, 50)
                      .ToList();
   productVM.Products = products.Where(a => a.CategoryId == categoryId);
   productVM.CategoryId=1 // ex. your value
   return View("Detay", productVM);
}

view:

@model ProductsViewModel
@if(Model.CategoryId==1).. // then you can use like this.

Note: you can do that without using viewmodel. in that case declare property to model class. assign value to that property and pass that model to the view.

Rashedul.Rubel
  • 3,446
  • 25
  • 36
-1

You can send the category id through ViewBag,

in Controller's Action:

ViewBag.CategoryId = categoryId;

in view:

@if (ViewBag.CategoryId == 1)
{

}
  • Using ViewBag in this case is not a very good idea, since it is not strongly typed. And this data clearly belongs to the model. – Alex Art. May 18 '15 at 06:57
  • yes i know that it's belong to model but it is very fast and clear way to perform the required task. – Mirza Danish Baig May 18 '15 at 06:59
  • Indeed, strongly typed solution is always preferred, which is also very fast and more clear. Don't use ViewBag. – L-Four May 18 '15 at 06:59
  • http://stackoverflow.com/questions/4766062/is-using-viewbag-in-mvc-bad http://stackoverflow.com/questions/11262034/mvc-viewbag-best-practice http://completedevelopment.blogspot.co.il/2011/12/stop-using-viewbag-in-most-places.html And there are many more... – Alex Art. May 18 '15 at 07:02