Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Everyone all of you are great..
i am new to MVC the problem which i am facing is as follows.
i have a Product Model and a Category Model and in my database i have two tables one is Product And other is Category that have relation with each other. i created a partial view in which i have a dropdownlist which is populated from Category Table using Category Model. And in addition to that dropdownlist i also have some other input fields like Product_Name, Product_Image, Product_Price which. what i want to do here is i want to insert the product model data as well as dropdownlist selected value into database.
basically i want to inset product data as well as category dropdownlist value into databse ... kindly show me some code or share with me some suggestions so i can get out of it. thankx in advance

What I have tried:

My Product Model
public class Products
    {
        public int P_ID { get; set; }
        public string P_Name { get; set; }
        public string P_Image { get; set; }
        public int P_Price { get; set; }
        public int P_CatedoryID { get; set; }
    }

My Category Model
public class Category
   {
       public int Cat_ID { get; set; }
       [Required]
       public string Cat_Name { get; set; }
   }


My Controller is as follows:

public ActionResult CreateProductsByAjaxCall()
    {
        ViewBag.Categories = db.GetALLCategoriesForProducts().Select(x => new SelectListItem { Text = x.Cat_Name, Value = x.Cat_ID.ToString() });
        return PartialView();
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult CreateProductsByAjaxCall(Products product)
    {
        if (ModelState.IsValid)
        {
            db.CreateProducts(product);
        }
        return RedirectToAction("Index");
    }


My Partial View Is As Follows

@model WebApplication.Models.Products
           <div class="container alert-success">
               <h4>Create Products</h4>
               @using (Html.BeginForm())
               {
                   @Html.AntiForgeryToken()
                       <div class="form-horizontal">
                           @Html.ValidationSummary(true, "", new { @class="text-danger"})
                           <div class="form-group">
                              @Html.LabelFor(model => model.P_Name, "Product Name:", htmlAttributes: new { @class="control-label col-md-2"})
                               <div class="col-lg-8">
                                   @Html.EditorFor(model => model.P_Name, new { htmlattributes = new { @class="form-control"} })
                                   @Html.ValidationMessageFor(model => model.P_Name, "", new { @class="text-danger"})
                               </div>
                           </div>
                           <div class="form-group">
                               @Html.LabelFor(model => model.P_Image, "Upload Image:", htmlAttributes: new { @class = "control-label col-md-2" })
                               <div class="col-lg-8">
                                   @Html.EditorFor(model => model.P_Image, new { htmlattributes = new { @class = "form-control" ,type="file"}  })
                                   @Html.ValidationMessageFor(model => model.P_Image, "", new { @class = "text-danger" })
                               </div>
                           </div>

                           <div class="form-group">
                               @Html.LabelFor(model => model.P_Price, "Product Unit Price:", htmlAttributes: new { @class = "control-label col-md-2" })
                               <div class="col-lg-8">
                                   @Html.EditorFor(model => model.P_Price, new { htmlattributes = new { @class = "form-control" } })
                                   @Html.ValidationMessageFor(model => model.P_Price, "", new { @class = "text-danger" })
                               </div>
                           </div>

                           <div class="form-group">
                               @Html.LabelFor(model => model.P_CatedoryID, "Product Category:", htmlAttributes: new { @class = "control-label col-md-2" })
                               <div class="col-lg-8">
                                 
                                 
                                   @Html.DropDownList("Categories", (SelectList)ViewBag.RequiredKey, new { @class = "form-control" })                               

                               </div>
                           </div>
                           <div class="form-group">
                               <div class="col-md-offset-2 col-md-2">
                                   <input type="submit" value=" Create" class="btn btn-success" />
                               </div>
                           </div>
                       </div> 
               }
           </div>
Posted
Updated 15-Mar-17 19:40pm
Comments
Sunasara Imdadhusen 15-Mar-17 3:38am    
So, what was the issue? i can see "@Html.LabelFor(model => model.P_CatedoryID, in the partial view. it means it will send complete product model with category id.
Muhammd Aamir 15-Mar-17 6:28am    
@Sunasara Imdadhusen, Sir its not matter of label i want to insert data from dropdownlist into database and i did not find any code that is reliable in solving my problem...
Sunasara Imdadhusen 15-Mar-17 6:42am    
Are you able to get all data inside this method CreateProductsByAjaxCall(Products product) when you click on Submit button?
Muhammd Aamir 16-Mar-17 1:18am    
Sir,when i debug my code it will display all the value that product have but the value of P_CategoryID is '0' Zero that is the main issue how i can get the exact value of the selected dropdownlist..

1 solution

Hi,
You can change the id of dropdown to "P_CatedoryID"
from
@Html.DropDownList("Categories", (SelectList)ViewBag.RequiredKey, new { @class = "form-control" })

to
@Html.DropDownList("P_CatedoryID", (SelectList)ViewBag.RequiredKey, new { @class = "form-control" })
 
Share this answer
 
Comments
Muhammd Aamir 16-Mar-17 2:02am    
Sir Your Solution is great now it works for me thank you very much sir you solve my problem that i have been facing from past 10 days ... cheers
Sunasara Imdadhusen 16-Mar-17 2:06am    
Thank you for appreciation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900