Auto Implemented Get属性如何返回特定数据成员

时间:2014-06-06 14:57:00

标签: c# asp.net-mvc radio-button

首先,我想告诉所有人我已经花了3天时间阅读材料和观看教程,并且仍然很难理解这个概念!请帮助我了解类的自动实现属性如何确定当您有多个数据成员时要返回的数据成员。以下是一个代码剪切,使用实体框架从SQL(具有3列的表; Id(int-which是PK),Name(nvarchar)和IsSelected(bit))服务器中提取简单数据并生成单选按钮。当您选择每个单选按钮并点击提交时,它会告诉您选择了哪个部门(ID)。

我的问题是,在此上下文中还有2个其他数据成员,Auto Implemented Get方法 SelectedDepartment 如何计算 Id 字段?

SQL服务器中的 Id 数据类型也是int,但为 SelectedDepartment 定义的数据类型是字符串!如何运作

非常感谢任何帮助!

模型(Company.cs);

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace RadioButton.Models
{
    public class Company
    {
        public string SelectedDepartment { get; set; }
        public List<Department> Departments
        {
            get
            {
                SampleContext db = new SampleContext();
                return db.Departments.ToList();
            }
        }

    }
}

控制器;

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RadioButton.Models;

namespace RadioButton.Controllers
{
    public class HomeController : Controller
    {
        private SampleContext db = new SampleContext();

        //
        // GET: /Home/
        [HttpGet]
        public ActionResult Index()
        {
            Company company = new Company();
            return View(company);
        }
        [HttpPost]
        public string Index(Company company)
        {
            if(string.IsNullOrEmpty(company.SelectedDepartment))
            {
                return "You didn't select any";
            }
            else
            {
                return "you have selected " + company.SelectedDepartment;
            }
        }

        //
        // GET: /Home/Details/5

        public ActionResult Details(int id = 0)
        {
            Department department = db.Departments.Find(id);
            if (department == null)
            {
                return HttpNotFound();
            }
            return View(department);
        }

        //
        // GET: /Home/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Home/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Department department)
        {
            if (ModelState.IsValid)
            {
                db.Departments.Add(department);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(department);
        }

        //
        // GET: /Home/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Department department = db.Departments.Find(id);
            if (department == null)
            {
                return HttpNotFound();
            }
            return View(department);
        }

        //
        // POST: /Home/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Department department)
        {
            if (ModelState.IsValid)
            {
                db.Entry(department).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(department);
        }

        //
        // GET: /Home/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Department department = db.Departments.Find(id);
            if (department == null)
            {
                return HttpNotFound();
            }
            return View(department);
        }

        //
        // POST: /Home/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Department department = db.Departments.Find(id);
            db.Departments.Remove(department);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}

查看;

@model RadioButton.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" />
}

1 个答案:

答案 0 :(得分:1)

属性只是值访问器;他们是愚蠢的,也就是说,这里没有魔法。最基本的属性基本上就是这个代码:

private string someProperty;
public string SomeProperty
{
    get { return someProperty; }
    set { someProperty = value; }
}

C#中的所有自动实现的属性都只是将public string SomeProperty { get; set; }转换为幕后的代码。该类别无法访问任何其他财产,且该财产不了解该类别的任何其他财产,也不关心。

Html.RadioButtonFor获取属性(通过表达式)和值,并使用它来构造无线电输入。最初,SelectedDepartment属性的实际值为空,但Html.RadioButtonFor未查看其值,而是将您在第二个参数中作为value属性提供的值HTML输入。在这种情况下,您使用的是Id属性,但这并不重要。同时,HTML输入的name值设置为“SelectedDepartment”,因为这是您告诉它绑定到的属性。

现在,当您回发时,输入的值将作为SelectedDepartment的值发送。它不知道或不关心该值最初来自Id属性。你可以轻松地做到:

@Html.RadioButtonFor(m => m.SelectedDepartment, "foo")

回发中SelectedDepartment的值将是“foo”。关键在于,与最初来源的位置没有直接关联,只是它是发布时HTML输入的值。