Skip to main content
added 81 characters in body
Source Link
Chris
  • 123
  • 4

Building on Uno's answer, I would recommend the following refactor for the controller action:

public ActionResult Category(int? id)
{
    // No need to create a pid variable if it's only 
    // used once here. Inline the null conditional.
    var category = db.Categories.Find(id ?? 1);
    
    if (category == null) 
    {
        // Assuming you have a global error handler attribute registered,
        // the condition where a requested category is not found is in fact
        // an exceptional state and the HTTP 404 status code is semantically
        // more intuitive than returning a generic error view.
        throw new HttpException(404, "Category not found.");
    }

    // I too tend to avoid returning IEnumerables outside of the context
    // because sometimes the enumeration depends on something that falls 
    // out of scope (like the data context). But I would typically avoid
    // the conversion to a list until the last possible moment.
    // If you need to come back and do some logic with the model later,
    // you can add it before returning the view and work directly with
    // the enumerable which is probably preferable in most cases.
    var model = category.Products;
    return View(model.ToList());
}

As for separating the category partial into a controller action:

Category list action:

public ActionResult CategoryList() 
{
    @using (var db = new WebShopEntities()) 
    {
        return PartialView("CategoryList", db.Categories.ToList());
    }
}

Partial view to go with:

@model List<Category>

foreach (var category in Model)
{
    <a href="@Url.Action("Category","Product", new {id = category.id})" class="list-group-item">@category.name</a>
}

You can render this in any view using

@Html.RenderAction("CategoryList")

Building on Uno's answer, I would recommend the following refactor for the controller action:

public ActionResult Category(int? id)
{
    // No need to create a pid variable if it's only 
    // used once here. Inline the null conditional.
    var category = db.Categories.Find(id ?? 1);
    
    if (category == null) 
    {
        // Assuming you have a global error handler attribute registered,
        // the condition where a requested category is not found is in fact
        // an exceptional state and the HTTP 404 status code is semantically
        // more intuitive than returning a generic error view.
        throw new HttpException(404, "Category not found.");
    }

    // I too tend to avoid returning IEnumerables outside of the context
    // because sometimes the enumeration depends on something that falls 
    // out of scope (like the data context). But I would typically avoid
    // the conversion to a list until the last possible moment.
    // If you need to come back and do some logic with the model later,
    // you can add it before returning the view and work directly with
    // the enumerable which is probably preferable in most cases.
    var model = category.Products;
    return View(model.ToList());
}

As for separating the category partial into a controller action:

Category list action:

public ActionResult CategoryList() 
{
    @using (var db = new WebShopEntities()) 
    {
        return PartialView("CategoryList", db.Categories.ToList());
    }
}

Partial view to go with:

@model List<Category>

foreach (var category in Model)
{
    <a href="@Url.Action("Category","Product", new {id = category.id})" class="list-group-item">@category.name</a>
}

Building on Uno's answer, I would recommend the following refactor for the controller action:

public ActionResult Category(int? id)
{
    // No need to create a pid variable if it's only 
    // used once here. Inline the null conditional.
    var category = db.Categories.Find(id ?? 1);
    
    if (category == null) 
    {
        // Assuming you have a global error handler attribute registered,
        // the condition where a requested category is not found is in fact
        // an exceptional state and the HTTP 404 status code is semantically
        // more intuitive than returning a generic error view.
        throw new HttpException(404, "Category not found.");
    }

    // I too tend to avoid returning IEnumerables outside of the context
    // because sometimes the enumeration depends on something that falls 
    // out of scope (like the data context). But I would typically avoid
    // the conversion to a list until the last possible moment.
    // If you need to come back and do some logic with the model later,
    // you can add it before returning the view and work directly with
    // the enumerable which is probably preferable in most cases.
    var model = category.Products;
    return View(model.ToList());
}

As for separating the category partial into a controller action:

Category list action:

public ActionResult CategoryList() 
{
    @using (var db = new WebShopEntities()) 
    {
        return PartialView("CategoryList", db.Categories.ToList());
    }
}

Partial view to go with:

@model List<Category>

foreach (var category in Model)
{
    <a href="@Url.Action("Category","Product", new {id = category.id})" class="list-group-item">@category.name</a>
}

You can render this in any view using

@Html.RenderAction("CategoryList")
Source Link
Chris
  • 123
  • 4

Building on Uno's answer, I would recommend the following refactor for the controller action:

public ActionResult Category(int? id)
{
    // No need to create a pid variable if it's only 
    // used once here. Inline the null conditional.
    var category = db.Categories.Find(id ?? 1);
    
    if (category == null) 
    {
        // Assuming you have a global error handler attribute registered,
        // the condition where a requested category is not found is in fact
        // an exceptional state and the HTTP 404 status code is semantically
        // more intuitive than returning a generic error view.
        throw new HttpException(404, "Category not found.");
    }

    // I too tend to avoid returning IEnumerables outside of the context
    // because sometimes the enumeration depends on something that falls 
    // out of scope (like the data context). But I would typically avoid
    // the conversion to a list until the last possible moment.
    // If you need to come back and do some logic with the model later,
    // you can add it before returning the view and work directly with
    // the enumerable which is probably preferable in most cases.
    var model = category.Products;
    return View(model.ToList());
}

As for separating the category partial into a controller action:

Category list action:

public ActionResult CategoryList() 
{
    @using (var db = new WebShopEntities()) 
    {
        return PartialView("CategoryList", db.Categories.ToList());
    }
}

Partial view to go with:

@model List<Category>

foreach (var category in Model)
{
    <a href="@Url.Action("Category","Product", new {id = category.id})" class="list-group-item">@category.name</a>
}