raju bhai
raju bhai

Reputation: 37

How to Insert a record in database using dropdownlist In asp.net mvc?

I m implement a drop-down list with entity framework database first approach in asp.net MVC??

I implement a drop-down list in MVC and when I M debugging In watch Window My Inserted Record Is display but Not Inserted In Database that is an issue??

DatabaseField Name:

tbl_product

tbl_product.cs

    // <auto-generated>
    // </auto-generated>
    public partial class tbl_product
    {
        public int ProductID { get; set; }
        public string ProductName { get; set; }
        public string ProductList { get; set; }

        public IEnumerable<SelectListItem> ProductLists { get; set; }
    }

HomeController.cs

        ProductEntities prodb = new ProductEntities();

        public ActionResult Index()
        {
            return View(prodb.tbl_product.ToList());
        }

        public ActionResult Prodlist()
        {

            var ProductLists = GetallProductList();

            var model = new tbl_product();


            model.ProductLists = GetSelectListItems(ProductLists);

            return View(model);
        }

        [HttpPost]
        public ActionResult Prodlist(tbl_product product)
        {
            var ProductLists = GetallProductList();

            product.ProductLists = GetSelectListItems(ProductLists);

            if (ModelState.IsValid)
            {
                prodb.tbl_product.Add(product);
                return RedirectToAction("Index");
            }
            return View("Prodlist", product);
        }

        private IEnumerable<SelectListItem> GetSelectListItems(IEnumerable<string> elements)
        {
            var selectList = new List<SelectListItem>();

            foreach (var element in elements)
            {
                selectList.Add(new SelectListItem
                {
                    Value = element,
                    Text = element
                });
            }
            return selectList;
        }

        private IEnumerable<string> GetallProductList()
        {
            return new List<string>
            {
                "FRIDGE",
                "WASHING MACHINE",
                "A.C",
            };
        }
    }

view:

Prodlist.cshtml

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

         //product list
        <div class="form-group">
            @Html.LabelFor(model => model.ProductList, htmlAttributes: new { @class = "control-label col-md-2" })
            <div>
                @Html.DropDownListFor(model => model.ProductList,Model.ProductLists,"Please Select Your Products:", new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ProductList, "", new { @class = "text-danger" })
            </div>
        </div>

Model1.Context.cs

    // <auto-generated>
    // </auto-generated>
    public partial class ProductEntities : DbContext
    {
        public ProductEntities()
            : base("name=ProductEntities")
        {
        }
        public virtual DbSet<tbl_product> tbl_product { get; set; }
    }

Index.cshtml


@model IEnumerable<DropdownInsertMvc.Models.tbl_product>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.ProductName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ProductList)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.ProductName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ProductList)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ProductID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ProductID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ProductID })
        </td>
    </tr>
}

</table>

When I m Debugging In Watch Window:

record is not inserted in the database??

Upvotes: 0

Views: 560

Answers (1)

Hamed Moghadasi
Hamed Moghadasi

Reputation: 1673

You missed prodb.SaveChanges() after prodb.tbl_product.Add(product); in Prodlist method.

for more information about SaveChanges() you can follow this link

Upvotes: 1

Related Questions