我想知道为什么他们在这里使用linq Expression 语句:
@Html.RadioButtonFor(m => m.SelectedDepartment, department.Id) @department.Name.How
财产的价值(公司类选定部门)
调用索引控制器的get事件时的设置以及值的方式
选择部门(公司类的财产)被检索。
Code:
//This a model class
public class Company
{
public string SelectedDepartment { get; set; } //Property
public List<Department> Departments //Property
{
get
{
SampleDBContext db = new SampleDBContext();
return db.Departments.ToList();
}
}
}
// This is a controller class
class HomeController
{
[HttpGet]
public ActionResult Index() //Controller Method1
{
Company company = new Company();
return View(company);
}
[HttpPost]
public string Index(Company company) //Controller Method2
{
if (string.IsNullOrEmpty(company.SelectedDepartment))
{
return "You did not select any department";
}
else
{
return "You selected department with ID = " + company.SelectedDepartment;
}
}
}
// This is a view
@model MVCDemo.Models.Company
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
foreach (var department in Model.Departments)
{
@Html.RadioButtonFor(m => m.SelectedDepartment, department.Id) @department.Name
}
<br />
<br />
<input type="submit" value="Submit" />
}