简体   繁体   English

如何将选定的ID从DropDownListFor传递到ASP.NET MVC中的控制器

[英]How to pass selected id from DropDownListFor to controller in asp.net mvc

I'm just a newbie and I have a problem to my DropDownList:(MVC, ASP.NET) 我只是新手,我的DropDownList有问题:(MVC,ASP.NET)

Scenario: 场景:

In View, when the dropdown is selected it display the value (Category Name) In Controller, instead of (category name) it will recognize only the assigned ID to the specific value 在“视图”中,选择下拉列表后,它将显示值(类别名称)。在控制器中,它将代替(类别名称)仅识别分配给特定值的ID

Problem: 问题:

when it click the button save, the return value from dropdownlist is NULL then in the CATEGORY table it create another row with an EMPTY content Model: (Supplier) 单击保存按钮时,下拉列表的返回值为NULL,然后在CATEGORY表中创建具有EMPTY内容的另一行型号:(供应商)

    public class Supplier
    {
        public int Id { get; set; }
        public string SupplierCode { get; set; }
        public string SupplierName { get; set; }
        public int SupplierContact { get; set; }
        public Category Category { get; set; }
    }

Model: (Category) 型号:(类别)

    public class Category
    {
        public int Id { get; set; }
        public string CatName { get; set; }
    }

Controller (Supplier) 控制器(供应商)

        public ActionResult New()
        {
            var CategoryMenu = _SBC.Categorys.ToList();
            var NewContent = new SupplierandCategoryViewModel()
            {
               CategoryModel = CategoryMenu,
            };
            return View(NewContent);
        }

        public ActionResult Save(SupplierandCategoryViewModel Supply)
        {   

            var DSupply = new Supplier()
            {
                SupplierName = Supply.SupplierModel.SupplierName,
                SupplierCode = Supply.SupplierModel.SupplierCode,
                SupplierContact = Supply.SupplierModel.SupplierContact,
                Category = Supply.CategoryModel //this part is error; it cannot 
                recognize
            };

            _SBC.Suppliers.Add(DSupply);
            _SBC.SaveChanges();
            return View();
        }

View: (Supplier) 查看:(供应商)

@model ShoeInformation.ViewModel.SupplierandCategoryViewModel
@{
    ViewBag.Title = "New";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<br />
<br />
<h2>Create New Customer</h2>
<br />
<br />
@using (Html.BeginForm("Save", "Supplier"))
{
    <div class="form-group">
        @Html.LabelFor(x=>x.CategoryModel)
        @Html.DropDownListFor(x => x.CategoryModel, new SelectList(Model.CategoryModel,"Id","CatName"), "Select Supplier Category", new {id="myCat",@class="form-control" })
    </div>

     <div class="form-group">
        @Html.LabelFor(x=>x.SupplierModel.SupplierCode)
        @Html.TextBoxFor(x => x.SupplierModel.SupplierCode, new { @class="form-control"})
    </div>
    <div class="form-group">
        @Html.LabelFor(x=>x.SupplierModel.SupplierName)
        @Html.TextBoxFor(x => x.SupplierModel.SupplierName, new { @class="form-control"})
    </div>
    <div class="form-group">
        @Html.LabelFor(x=>x.SupplierModel.SupplierContact)
        @Html.TextBoxFor(x => x.SupplierModel.SupplierContact, new { @class="form-control"})
    </div>

}

ViewModel: (SupplierandCategoryViewModel) ViewModel:(SupplierandCategoryViewModel)

    public class SupplierandCategoryViewModel
    {
        public IEnumerable<Category> CategoryModel { get; set; }
        public Supplier SupplierModel { get; set; }
    }

I want to save the ID of category but in view(Index) it must display the value of ID not the ID itself 我想保存类别的ID,但是在view(Index)中它必须显示ID的值而不是ID本身

Thanks for your Response!! 感谢您的答复!!

1) Add one SelectedCategory property to your SupplierandCategoryViewModel 1)将一个SelectedCategory属性添加到您的SupplierandCategoryViewModel

public class SupplierandCategoryViewModel
{
    public IEnumerable<Category> CategoryModel { get; set; }
    public Supplier SupplierModel { get; set; }
    public int SelectedCategory { get; set; }     <== Add this property
}

2) Provide proper name for Id 2)提供Id专有名称

public class Category
{
    public int CategoryId { get; set; }    <== Change the name
    public string CatName { get; set; }
}

3) Reference this CategoryId to your Supplier model 3)将此CategoryId引用到您的Supplier模型

public class Supplier
{
    public int Id { get; set; }
    public string SupplierCode { get; set; }
    public string SupplierName { get; set; }
    public int SupplierContact { get; set; }
    public int CategoryId { get; set; }        <== Reference to this property
    public Category Category { get; set; }
}

4) Modify your DropDownListFor like 4)修改你的DropDownListFor

@Html.DropDownListFor(x => x.SelectedCategory, new SelectList(Model.CategoryModel, "CategoryId", "CatName"), "Select Supplier Category", new { id = "myCat", @class = "form-control" })

5) Finally your Save action method like 5)最后,您的Save操作方法如下

public ActionResult Save(SupplierandCategoryViewModel Supply)
{

    var DSupply = new Supplier()
    {
        SupplierName = Supply.SupplierModel.SupplierName,
        SupplierCode = Supply.SupplierModel.SupplierCode,
        SupplierContact = Supply.SupplierModel.SupplierContact,
        CategoryId = Supply.SelectedCategory                     <==Note here I assigned SelectedCategory to model's CategoryId
    };

    _SBC.Suppliers.Add(DSupply);
    _SBC.SaveChanges();
    return View();
}

Test Data for DropDownList: DropDownList的测试数据:

var CategoryMenu = new List<Category> { new Category { CategoryId = 1, CatName = "Abc" }, new Category { CategoryId = 2, CatName = "Pqr" } };

Output: 输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC - 如何将所选项目从 DropDownListFor 传输到 Controller - ASP.NET MVC - How to transfer the selected item from DropDownListFor to the Controller ASP.NET MVC DropDownListFor传递给控制器​​的类型 - ASP.NET mvc DropDownListFor pass type to controller asp.net mvc Html.DropDownListFor:如何处理选定的ID - asp.net mvc Html.DropDownListFor: how to handle selected id 如何将 ID 传递给 ASP.NET MVC 中的控制器? - How do I pass an ID to the controller in ASP.NET MVC? Asp.Net MVC:如何在提交时从 DropDownListFor 中获取选定的选项 - Asp.Net MVC:How to get selected option from DropDownListFor in Action on submit 如何从控制器 ASP.NET MVC 的视图页面中自动添加所选 id 的名称 - How to automatically add a name from selected id in View page from controller ASP.NET MVC 如何在ASP.Net MVC实体框架的帐户控制器中传递来自登录方法的ID? - How can I pass the Id from Login method in Account Controller in ASP.Net MVC Entity Framework? 如何在 asp.net mvc 中创建 DropdownlistFor? - How to create a DropdownlistFor in asp.net mvc? 如何将数据从 Controller 传递到 ASP.NET MVC 中的 WebForm? - How to pass data from Controller to WebForm in ASP.NET MVC? 如何将数据从控制器传递到 ASP.NET MVC 中查看 - How to to pass data from controller to view in ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM