他们为什么在这里使用linq Expression @ Html.RadioButtonFor(m => m.SelectedDepartment,department.Id)@ department.Name?

时间:2015-07-29 09:22:36

标签: asp.net-mvc-4

我想知道为什么他们在这里使用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" />
        }

0 个答案:

没有答案