0

Im trying to join two tables and show a table from those joined results , as well as show a create form the same view

I just followed following questions answer

here these are the my model classes

public class CategoryViewModel
{
    public AB_ProductTypeCategory categoryCreate { get; set; }
    public IEnumerable<AB_ProductTypeCategory> categoryList { get; set; }
}

public partial class AB_ProductTypeCategory
{
    public string ProductCategoryID { get; set; }
    public string ProductTypeID { get; set; }
    public string ProductCategoryNameEn { get; set; }    
}

these are the controller methods

Join LINQ query to get the list

   [HttpGet]
    public ViewResult ProductCategory_List()
    {
        var catagorylist =  from cat in db.AB_ProductTypeCategory
                             join typ in db.AB_ProductType  on cat.ProductTypeID equals typ.ProductTypeID
                             select new AB_ProductTypeCategory
                             {

                              ProductCategoryID = cat.ProductCategoryID,
                              ProductCategoryNameEn = cat.ProductCategoryNameEn,                                 
                              ProductTypeID= typ.ProductTypeID,                                 

                             };

        return View(catagorylist);

this is create controller method

    [HttpGet]
    public ActionResult ProductCategory_Create()
    {
        Product_Type_DropDownListEn();
        Product_Type_DropDownListAr();

        return View();
    }

    [HttpPost]
    public ActionResult ProductCategory_Create(AB_ProductTypeCategory product_category)
    {
        Product_Type_DropDownListEn(product_category.ProductTypeID);
        Product_Type_DropDownListAr(product_category.ProductTypeID);

        if (ModelState.IsValid)
        {
            ..
        }

        return View(product_category);
    }

this is List viewpage "ProductCategory_List"

@model IEnumerable<project_name.Models.AB_ProductTypeCategory>


<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.ProductCategoryID)
        </th>
     ...

this is page Im trying to show list view and create view

@model project_name.Models.AB_ProductTypeCategory

@{    
}

@{Html.RenderAction("ProductCategory_List", "Home");}


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Create New Product Category</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.ProductCategoryID, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ProductCategoryID, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ProductCategoryID, "", new { @class = "text-danger" })
            </div>
        </div>
        .....
Community
  • 1
  • 1
kez
  • 2,273
  • 9
  • 64
  • 123
  • If you wanting to display both, then you view needs to be `CategoryViewModel` and you need to return an instance of `CategoryViewModel` to the view (or you need to use `@Html.Action()` to call separate controller methods that render partial views. –  Feb 29 '16 at 11:38
  • I cannot understand what you said , can I have little example – kez Feb 29 '16 at 11:41
  • Just do it exactly as per the question you linked to (not the answer!) but pass an instance of the model to the view - in that question, it should have been `var model = new EventoViewModel { LEventos = db.Eventos.ToList() }; return View(model);` –  Feb 29 '16 at 11:49
  • are you saying smothing like this `public ViewResult List() { var model = new EventoViewModel { LEventos = db.Eventos.ToList() }; return View(model); }` – kez Feb 29 '16 at 11:54
  • @StephenMuecke are you saying without create new view page for list , just embed both code in same view page ? – kez Feb 29 '16 at 11:56
  • Yes, that's one option. The other is to to use `@Html.Action()` or `@Html.Partial()` a per the answer in the linked question. There are many ways to do it. –  Feb 29 '16 at 11:58
  • can I now the way to do it with separate view pages , one for list and then embed that in create view ? – kez Feb 29 '16 at 12:01
  • Yes, using `@Html.Action()` or `@Html.Partial()` –  Feb 29 '16 at 12:02
  • I just change to `@{Html.Action("ProductCategory_List", "Home");}` but then getting error as `The entity or complex type 'project_name.AB_ProductTypeCategory' cannot be constructed in a LINQ to Entities query.` – kez Feb 29 '16 at 12:05
  • You you have an error in your query :) –  Feb 29 '16 at 12:12
  • @StephenMuecke are you saying `var catagorylist` query wrong here ? – kez Mar 01 '16 at 10:22
  • Yes, (you cannot project into a mapped entity). Create a view model (say) `ProductTypeCategoryVM` with those 3 property and use `select new ProductTypeCategoryVM` (and then you will need change the properties in your `CategoryViewModel` to use `ProductTypeCategoryVM` as well –  Mar 01 '16 at 10:33
  • Is this correct [entity approach](https://bitbucket.org/snippets/Common_Admin/ER56n) – kez Mar 01 '16 at 10:44
  • Yes that should work fine. And I just found [these answers](http://stackoverflow.com/questions/5325797/the-entity-cannot-be-constructed-in-a-linq-to-entities-query) which explain the issue in more detail –  Mar 01 '16 at 10:46
  • @StephenMuecke I just changed [as follows](https://bitbucket.org/snippets/Common_Admin/ER56n) view and list , but still getting errors – kez Mar 01 '16 at 11:11
  • But where are you using `CategoryViewModel`? And what errors? –  Mar 01 '16 at 11:17
  • error is `System.NotSupportedException: The entity or complex type 'project_name.AB_ProductTypeCategory' cannot be constructed in a LINQ to Entities query.` at this line `@foreach (var item in Model) {` of list view , where should I place `CategoryViewModel` Create page or List page – kez Mar 01 '16 at 11:32
  • That means you still using `select new AB_ProductTypeCategory` instead of `select new ProductTypeCategoryVM` as per my previous comments. And I don't know where you should use `CategoryViewModel` - you included it in your code but never seem to use it –  Mar 01 '16 at 11:34
  • I just changed [like this](https://bitbucket.org/snippets/Common_Admin/ER56n) then getting errors in create page as follows `'CategoryViewModel' does not contain a definition for 'ProductCategoryID' and no extension method 'ProductCategoryID' accepting a first argument of type 'CategoryViewModel' could be found` – kez Mar 01 '16 at 11:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105000/discussion-between-stephen-muecke-and-kez). –  Mar 01 '16 at 11:42
  • @StephenMuecke for create post method which model class should I use ? `CategoryViewModel` or `ProductTypeCategoryVM` – kez Mar 02 '16 at 04:15
  • Just `ProductTypeCategoryVM` (I don't really see any need for your `CategoryViewModel` and it could be deleted, but then again I'm not sure if you use it somewhere else) –  Mar 02 '16 at 04:22
  • then in this line `db.AB_ProductTypeCategory.Add(product_category);` error is occurring as Severity Code Description Project File `Argument 1: cannot convert from 'project_name.Models.ProductTypeCategoryVM' to 'project_name.Models.AB_ProductTypeCategory'` – kez Mar 02 '16 at 04:25
  • I cant find that line of code in your question :). But I assume its in the POST method for create which should no look like `public ActionResult ProductCategory_Create(ProductTypeCategoryVM product_category)` and you cannot add `ProductTypeCategoryVM` to the `AB_ProductTypeCategory` table. You need in initialize a new instance of `AB_ProductTypeCategory` and map the properties from the view model to the data model and save the data model. –  Mar 02 '16 at 04:30
  • as you said I changed it, now I can see values coming , but in this createform I have dropdown also like this `@Html.DropDownListFor(m => m.ProductCategoryTypeEn, (SelectList)ViewBag.Product_TypeListEn, "Select Product Type", new { @class = "form-control" })` that selected Id not binding here to `ProductTypeCategoryVM` model `ProductTypeID` property – kez Mar 02 '16 at 04:48
  • This comment thread is getting out of hand. You need to to ask a new question and show the relevant code you now using, which is different from the code in the question (and I cant keep jumping back and forth from the links you have previously given), and I don't even know your populating the `ViewBag.Product_TypeListEn` –  Mar 02 '16 at 04:53
  • okay , Ill do to that :) – kez Mar 02 '16 at 04:55

0 Answers0