使用模型从数据库填充下拉列表,并使用单独的模型在数据库中插入该DDL的选定值...? [英] Populate a dropdownlist from database using model and insert the selected value of that DDL in database using separate model...?

查看:64
本文介绍了使用模型从数据库填充下拉列表,并使用单独的模型在数据库中插入该DDL的选定值...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,各位大家都很棒。

i我是MVC的新手,我面临的问题如下:

i有产品型号和类别模型在我的数据库中,我有两个表,一个是产品,另一个是与彼此有关系的类别。我创建了一个局部视图,其中我有一个下拉列表,使用类别模型从类别表中填充。除了该下拉列表,我还有一些其他输入字段,如Product_Name,Product_Image,Product_Price。我想在这里做的是我想将产品模型数据以及下拉列表选择的值插入到数据库中。

基本上我想将产品数据以及类别下拉列表值插入数据库...请告诉我一些代码或与我分享一些建议,以便我可以摆脱它。谢谢你提前



我尝试过:



我的产品型号

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>

推荐答案



你可以将下拉列表的ID更改为P_CatedoryID

来自

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



to


to

@Html.DropDownList("P_CatedoryID", (SelectList)ViewBag.RequiredKey, new { @class = "form-control" })


这篇关于使用模型从数据库填充下拉列表,并使用单独的模型在数据库中插入该DDL的选定值...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆