Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在asp.net mvc中将所选id从DropDownListFor传递给控制器_C#_Asp.net Mvc - Fatal编程技术网

C# 如何在asp.net mvc中将所选id从DropDownListFor传递给控制器

C# 如何在asp.net mvc中将所选id从DropDownListFor传递给控制器,c#,asp.net-mvc,C#,Asp.net Mvc,我只是个新手,我的下拉列表有个问题:MVC,ASP.NET 情景: 在视图中,选择下拉列表后,将显示值类别名称 在控制器中,它将只识别指定给特定值的ID,而不是类别名称 问题: 单击“保存”按钮时,dropdownlist的返回值为空 然后在CATEGORY表中创建另一个包含空内容的行 型号:供应商 @model ShoeInformation.ViewModel.SupplierandCategoryViewModel @{ ViewBag.Title = "New"; Lay

我只是个新手,我的下拉列表有个问题:MVC,ASP.NET

情景:

在视图中,选择下拉列表后,将显示值类别名称 在控制器中,它将只识别指定给特定值的ID,而不是类别名称

问题:

单击“保存”按钮时,dropdownlist的返回值为空 然后在CATEGORY表中创建另一个包含空内容的行 型号:供应商

@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>

}
型号:类别

    public class Category
    {
        public int Id { get; set; }
        public string CatName { get; set; }
    }
控制器供应商

        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();
        }
视图:供应商

@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>

}
视图模型:供应商和类别视图模型

    public class SupplierandCategoryViewModel
    {
        public IEnumerable<Category> CategoryModel { get; set; }
        public Supplier SupplierModel { get; set; }
    }
我想保存类别的ID,但在viewIndex中,它必须显示ID的值,而不是ID本身

谢谢你的回复

1将一个SelectedCategory属性添加到SupplierandCategoryViewModel中

    public class SupplierandCategoryViewModel
    {
        public IEnumerable<Category> CategoryModel { get; set; }
        public Supplier SupplierModel { get; set; }
    }
3将此类别ID引用到您的供应商型号

5最后你的保存动作方法如下

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();
}
DropDownList的测试数据:

输出:


是的,你的建议很有效。我可以补充一个问题吗。。。。在Supplier Controller中,如果我将类别模型声明为供应商模型的一部分,我如何添加SelectedCategory?是的,我将添加一个答案。请查看:您无法将一个绑定到复杂对象或对象集合,这就是CategoryModel-它绑定到,并发回一个简单值,即您需要绑定到int属性。视图模型不包含数据模型-它们包含您在视图中需要的数据模型属性,在您的案例中,还包含一个int-SelectedCategory和一个IEnumerable CategoryOptions属性,用于DropDownList formethod@Jhesie,查看答案可能对您有帮助:
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; }
}
@Html.DropDownListFor(x => x.SelectedCategory, new SelectList(Model.CategoryModel, "CategoryId", "CatName"), "Select Supplier Category", new { id = "myCat", @class = "form-control" })
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();
}
var CategoryMenu = new List<Category> { new Category { CategoryId = 1, CatName = "Abc" }, new Category { CategoryId = 2, CatName = "Pqr" } };