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>