[英]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.