【发布时间】:2014-07-27 21:41:10
【问题描述】:
首先我想告诉大家,我已经花了 3 天时间阅读材料和观看教程,但仍然很难理解这个概念!请帮助我了解当您有多个数据成员时,类的自动实现属性如何决定返回哪个数据成员。以下是一段代码,它使用实体框架从 SQL(一个有 3 列的表;Id(int-即 PK)、Name(nvarchar)和 IsSelected(bit))服务器中提取简单数据并生成单选按钮。当您选择每个单选按钮并点击提交时,它会告诉您您选择了哪个部门 (ID)。
我的问题是,在此上下文中还有 2 个其他数据成员,自动实现的 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" />
}
【问题讨论】:
-
如果你问我认为你在问什么,
@Html.RadioButtonFor(m => m.SelectedDepartment, department.Id)是这里的主要声明。 -
@JoachimIsaksson 谢谢你,这是否意味着 SelectedDepartment 属性一旦实例化就会包含其中的所有成员?然后我们可以在视图中使用我们想要的任何成员?
标签: c# asp.net-mvc radio-button