如何将ID od下拉保存到数据库中 [英] How to save the ID od dropdown to database
问题描述
大家好,我只是个新手,我的DropDownList有问题:(MVC,ASP.NET)
场景:
>在视图中,当选择下拉列表时,它会显示值(类别名称)
>在Controller中,而不是(类别名称)它将只识别指定ID到特定值
问题:
>当它点击按钮保存时,下拉列表中的返回值为NULL
>然后在CATEGORY表中创建另一行,其中包含EMPTY内容
型号:(供应商)
Hi Everyone, I'm just a newbie and I have a problem to my 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
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)
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:(
ViewModel: (
SupplierandCategoryViewModel
)
)
public class SupplierandCategoryViewModel
{
public IEnumerable<Category> CategoryModel { get; set; }
public Supplier SupplierModel { get; set; }
}
我想保存类别的ID但在视图(索引)中它必须显示ID的值不是身份证本身
感谢您的回复!!
我的尝试:
我尝试使用Supply的模型,但问题是它无法列出类别的内容
I want to save the ID of category but in view(Index) it must display the value of ID not the ID itself
Thanks for your Response!!
What I have tried:
I try to used the model of Supply but the problem it cannot list the content of category
推荐答案
你好像很困惑。
SupplierandCategoryViewModel
CategoryModel
是类别列表;SupplierModel.Category
是选定的类别;
You seem to be getting very confused.
SupplierandCategoryViewModel
CategoryModel
is the list of categories;SupplierModel.Category
is the selected category;
@Html.DropDownListFor(x => x.CategoryModel, ...
您正在尝试渲染下拉列表以选择整个类别列表。您应该选择单个而不是类别。
控制器:
You are trying to render a drop-down list to select the entire list of categories. You should be selecting the single category instead.
Controller:
public ActionResult Save(SupplierandCategoryViewModel Supply)
{
...
Category = Supply.CategoryModel
您正试图存储列表类别在一个只能存储单个类别的房产。
从简化开始您的视图模型:
You are trying to store the list of categories in a property which can only store a single category.
Start by simplifying your view model:
public class SupplierandCategoryViewModel
{
// The properties which will be edited:
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; }
// The data for any drop-down lists:
public IEnumerable<Category> AllCategories { get; set; }
}
然后更新你的行动:
public ActionResult New()
{
var viewModel = new SupplierandCategoryViewModel
{
AllCategories = _SBC.Categorys.ToList(),
};
return View(viewModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Save(SupplierandCategoryViewModel viewModel)
{
Supplier supplier;
if (viewModel.Id == 0)
{
supplier = new Supplier();
_SBC.Suppliers.Add(supplier);
}
else
{
supplier = _SBC.Suppliers.Single(s => s.Id == viewModel.Id);
}
supplier.SupplierName = viewModel.SupplierName;
supplier.SupplierCode = viewModel.SupplierCode;
supplier.SupplierContact = viewModel.SupplierContact;
supplier.Category = _SBC.Categorys.Single(c => c.Id == viewModel.CategoryId);
_SBC.SaveChanges();
return View();
}
然后您的表单变为:
...
@using (Html.BeginForm("Save", "Supplier"))
{
@Html.AntiForgeryToken()
<div class="form-group">
@Html.LabelFor(x => x.CategoryId)
@Html.DropDownListFor(x => x.CategoryId, new SelectList(Model.AllCategories, "Id", "CatName"), "Select Supplier Category", new { id="myCat", @class="form-control" })
</div>
<div class="form-group">
@Html.LabelFor(x => x.SupplierCode)
@Html.TextBoxFor(x => x.SupplierCode, new { @class="form-control" })
</div>
<div class="form-group">
@Html.LabelFor(x => x.SupplierName)
@Html.TextBoxFor(x => x.SupplierName, new { @class="form-control" })
</div>
<div class="form-group">
@Html.LabelFor(x => x.SupplierContact)
@Html.TextBoxFor(x => x.SupplierContact, new { @class="form-control" })
</div>
<p><button type="submit" class="btn btn-primary">Save</button></p>
}
您可能希望为视图模型添加一些验证属性:
使用数据注释验证器进行验证( C#)| Microsoft Docs [ ^ ]
这篇关于如何将ID od下拉保存到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!