Asp.net mvc MVC MultipleModel DropDownList for SaveChanges()
您好,我有一个多模式视图,其中包含CompanyName和EmployeeRange。CompanyName包含具有到EmployeeRange(int)字段的关系链接的公司名称 员工范围基本上是Asp.net mvc MVC MultipleModel DropDownList for SaveChanges(),asp.net-mvc,Asp.net Mvc,您好,我有一个多模式视图,其中包含CompanyName和EmployeeRange。CompanyName包含具有到EmployeeRange(int)字段的关系链接的公司名称 员工范围基本上是 0-9 10-19 20-49 我可以为新的公司名称字段创建和保存更改。 请帮助将所选值从EmployeeRange DropDownList for保存到DB 这是密码 //MultipleModel.cs public partial class MultipleModel
- 0-9
- 10-19
- 20-49
//MultipleModel.cs
public partial class MultipleModel
{
public MultipleModel()
{
CompanyEntities = new company();
EmployeeEntities = new Employee();
}
public company CompanyEntities { get; set; }
public Employee EmployeeEntities { get; set; }
}
//CompanyController.cs
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult RegisterCompany(MultipleModel model)
{
if (ModelState.IsValid)
{
//GET VARIBALE FROM DB
var addComanyName = db.companies.Add(model.CompanyEntities);
//GET VARIABLLE
addComanyName.COMPANY_NAME = model.CompanyEntities.COMPANY_NAME;
//ADD VARIBALE TO DB
db.companies.Add(addComanyName);
db.SaveChanges();
return RedirectToAction("index");
}
return View();
}
//RegisterCompany.cshtml
@model EISystem.Models.MultipleModel
@Html.DropDownListFor(m => m.CompanyEntities, new SelectList(ViewBag.products, "Employees_Range_ID", "Employees_Range"), "Select Number of Employees")
???如何查看DropDownListFor,以便所选值可以发布到控制器,然后保存到DB?对于MultipleModel类,您应该使用另一个类,即ViewModel 比如:
什么
DropDownListFor()
?它绑定到什么属性。显示您的视图。很抱歉,这实际上是一个员工范围列表,我只能通过Viewbag“Viewbag.products=db.Employees.ToList();“您不能将
绑定到复杂对象(这就是公司实体
是什么)-您需要绑定到的属性是int
(假设Employees\u Range\u ID
也为int
)是的Employees\u Range\u ID为int。这是一个多模式视图,因此所选内容必须保存在公司数据库(CompanyEntities)中.Nicee!!那么我该如何在视图中调用该类。Wudn这不排除初始类吗?。可能现在我对此很清楚。请参阅更新。在发送MultipleModelViewModel类以查看之前,显然必须先初始化它。在声明它之前,我不能使用局部变量(模型)。这是在第一个ActionResult RegisterCompany()中。为什么会这样说“在声明局部变量(model)之前,我不能使用它。”这里是“Name=model.Name”,您是否像示例中那样操作?在曲线括号内。var model=new MultipleModelViewModel(){Name=model.Name,Products=db.Products.Select(x=>new{Id=x.Id,Name=x.Name)。ToList();
public class MultipleModelViewModel
{
public int SelectedProductId { get; set; }
public List<Products> ProductList{ get; set; }
public string Name { get; set; }
}
@model MultipleModelViewModel
@using (Html.BeginForm("RegisterCompany", "Company", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-group">
@Html.LabelFor(model => model.SelectedProductId , htmlAttributes: new { @class = "form-control" })
@Html.DropDownListFor(model => model.SelectedProductId, new SelectList(Model.ProductList, "Id", "Name"), "Select Product", new { @class = "form-control " })
</div>
<div class="form-group">
@Html.LabelFor(model => model.Name, new { @class = "form-control " })
@Html.TextBoxFor(model => model.Name, new { @class = "form-control" })
</div>
<button type="Submit" class="btn btn-success " id="Save-btn">
Save
</button>
}
//Get
public ActionResult RegisterCompany()
{
var model = new MultipleModelViewModel (){
Name = model.Name,
Products = db.Products.Select(x=>new {Id=x.Id, Name=x.Name).ToList()
};
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult RegisterCompany(MultipleModelViewModel model)
{
if (ModelState.IsValid)
{
var company = new Company(){
Name = model.Name,
Product = db.Products.Find(model.SelectedProductId)
};
db.companies.Add(company);
db.SaveChanges();
return RedirectToAction("index");
}
return View();
}