Get属性返回特定的数据成员
本文关键字:数据成员 属性 返回 Get | 更新日期: 2023-09-27 18:07:32
首先我想告诉大家,我已经花了3天时间阅读材料和观看教程,但仍然很难理解这个概念!请帮助我了解一个类的自动实现属性如何决定什么数据成员返回时,你有多个数据成员。下面是一个代码片段,从SQL中提取简单的数据(一个3列的表;Id(int-即PK)、Name(nvarchar)和IsSelected(bit))服务器使用实体框架并生成单选按钮。当您选择每个单选按钮并点击提交时,它会告诉您选择了哪个Department (ID)。
我的问题是,在这个上下文中还有其他2个数据成员,如何自动实现Get方法SelectedDepartment数字取Id字段?
Id SQL服务器中的数据类型是int,但是为SelectedDepartment定义的数据类型是string !它是如何工作的
任何帮助都非常感谢!
模型(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" />
}
属性只是值访问器;他们很蠢,也就是说,这里没有魔法。最基本的属性基本上就是这段代码:
private string someProperty;
public string SomeProperty
{
get { return someProperty; }
set { someProperty = value; }
}
c#中所有自动实现的属性都是简单地将public string SomeProperty { get; set; }
转换为上述代码。不能访问该类上的任何其他属性,并且该属性不知道该类上的任何其他属性,也不关心。
Html.RadioButtonFor
接受一个属性(通过表达式)和一个值,并使用它来构造一个单选输入。最初,您的SelectedDepartment
属性的实际值为null,但Html.RadioButtonFor
不查看它的值,而是您在第二个参数中作为HTML输入的value
属性提供的值。在本例中,您使用的是Id
属性,但这并不重要。同时,HTML输入的name
值被设置为"SelectedDepartment",因为这是您告诉它要绑定的属性。
现在,当您发回时,输入的值作为SelectedDepartment
的值发送。它不知道或关心最初的值来自Id
属性。您也可以简单地这样做:
@Html.RadioButtonFor(m => m.SelectedDepartment, "foo")
且回发时SelectedDepartment
的值为"foo"。关键是,这个值与最初的来源没有直接的关系,只是它是发布时HTML输入的值。