MVC ASP C#我的设备ID未显示在我的视图上
我很难让我的UserID和DeviceID(两个独立模型的一部分)显示在我的一个索引视图上。Im正在构建一个网站,通过创建一个用户(又称患者),您可以在其中添加一个可以与该用户关联的设备。我通过种子设备列表的下拉列表来实现这一点。虽然下拉列表显示设备,但不会使用所选设备更新索引 我的目标是让DeviceID&Name正确显示在我的用户(也称为患者)索引视图上。此外,当我创建和编辑用户时,我希望下拉列表更新患者(用户)索引视图上的用户信息 PatientController.csMVC ASP C#我的设备ID未显示在我的视图上,c#,asp.net-mvc,linq,asp.net-mvc-4,razor,C#,Asp.net Mvc,Linq,Asp.net Mvc 4,Razor,我很难让我的UserID和DeviceID(两个独立模型的一部分)显示在我的一个索引视图上。Im正在构建一个网站,通过创建一个用户(又称患者),您可以在其中添加一个可以与该用户关联的设备。我通过种子设备列表的下拉列表来实现这一点。虽然下拉列表显示设备,但不会使用所选设备更新索引 我的目标是让DeviceID&Name正确显示在我的用户(也称为患者)索引视图上。此外,当我创建和编辑用户时,我希望下拉列表更新患者(用户)索引视图上的用户信息 PatientController.cs using Sy
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using FaceToFace.Model;
using System.Collections;
using System.ComponentModel.DataAnnotations;
namespace FaceToFaceWebsite.Controllers
{
public class PatientController : Controller
{
private F2FData db = new F2FData();
//
// GET: /Patient/
public ActionResult Index()
{
return View(db.Users.ToList());
}
//
// GET: /Patient/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
User user = db.Users.Find(id);
if (user == null)
{
return HttpNotFound();
}
return View(user);
}
// GET: /Patient/Create
public ActionResult Create()
{
ViewBag.DeviceID = new SelectList(db.Devices, "DeviceID", "Name");
return View();
}
//
// POST: /Patient/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "UserID,CodeName,UseBriefInstructions")] User user, Device device)
{
if (ModelState.IsValid)
{
//db.Devices = user.Device.DeviceID;
db.Users.Add(user);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.DeviceID = new SelectList(db.Devices, "DeviceID", "Name", user.Device.DeviceID);
return View(user);
}
//
// GET: /Patient/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
User user = db.Users.Find(id);
if (user == null)
{
return HttpNotFound();
}
//removed user.Device.DeviceID
ViewBag.DeviceID = new SelectList(db.Devices, "DeviceID", "Name");
return View(user);
}
//
// POST: /Patient/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "UserID,CodeName,UseBriefInstructions")] User user)
{
if (ModelState.IsValid)
{
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.DeviceID = new SelectList(db.Devices, "DeviceID", "Name", user.Device.DeviceID);
return View(user);
}
//
// GET: /Patient/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
User user = db.Users.Find(id);
if (user == null)
{
return HttpNotFound();
}
return View(user);
}
//
// POST: /Patient/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
User user = db.Users.Find(id);
db.Users.Remove(user);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.WebPages.Html;
using FaceToFace.Model;
namespace FaceToFaceWebsite.Models
{
public class UserDeviceViewModel
{
public UserDeviceViewModel()
{
User = new User();
Users = new List<User>();
Devices = new List<SelectListItem>();
}
public User UseBriefInstructions { get; set; }
public Device Device { get; set; }
public User User { get; set; }
public User CodeName { get; set; }
public IList<SelectListItem> Devices { get; set; }
public IList<User> Users { get; set; }
}
}
namespace FaceToFaceWebsite.Controllers
{
public class PatientController : Controller
{
private F2FData db = new F2FData();
//
// GET: /Patient/
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(UserDeviceViewModel viewModelDevice)
{
var viewModel = new UserDeviceViewModel();
viewModel.Devices.Add(new SelectListItem() { Text = "MAU110-10", Value = "1" });
viewModel.Devices.Add(new SelectListItem() { Text = "MAU110-100", Value = "2" });
viewModel.Devices.Add(new SelectListItem() { Text = "MAU110-101", Value = "3" });
viewModel.Devices.Add(new SelectListItem() { Text = "MAU110-102", Value = "4" });
viewModel.Devices.Add(new SelectListItem() { Text = "MAU110-103", Value = "5" });
viewModel.Users.AddRange(db.Users.ToList());
return View(viewModel);
}
User.cs(型号)
Device.cs(型号)
视图/Patient/Index.cshtml
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FaceToFace.Model
{
public class User
{
public int UserID { get; set; }
public string CodeName { get; set; }
public bool UseBriefInstructions { get; set; }
public Device Device { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FaceToFace.Model
{
public class Device
{
public int DeviceID { get; set; }
public string Name { get; set; }
}
}
@model IEnumerable<FaceToFace.Model.User>
@{
ViewBag.Title = "Index";
}
<h2>Your Patients</h2>
Showing @Model.Count() users
<p>
@Html.ActionLink("Add New User", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.UserID)
</th>
<th>
@Html.DisplayNameFor(model => model.CodeName)
</th>
<th>
@*@Html.DisplayNameFor(model => model.Device.Name)*@Device
</th>
<th>
@Html.DisplayNameFor(model => model.Device.DeviceID)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserID)
</td>
<td>
@Html.DisplayFor(modelItem => item.CodeName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Device.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Device.DeviceID)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.UserID }) |
@Html.ActionLink("Details", "Details", new { id = item.UserID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.UserID })
</td>
</tr>
}
</table>
@model FaceToFace.Model.User
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>User</legend>
<div class="editor-label">
@Html.LabelFor(model => model.CodeName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CodeName)
@Html.ValidationMessageFor(model => model.CodeName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Device.Name, "Device")
</div>
<div class="editor-field">
@Html.DropDownList("DeviceID", String.Empty)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UseBriefInstructions)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UseBriefInstructions)
@Html.ValidationMessageFor(model => model.UseBriefInstructions)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@model FaceToFace.Model.User
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>User</legend>
@Html.HiddenFor(model => model.UserID)
<div class="editor-label">
@Html.LabelFor(model => model.CodeName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CodeName)
@Html.ValidationMessageFor(model => model.CodeName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Device.Name, "Device")
</div>
<div class="editor-field">
@Html.DropDownList("DeviceID", String.Empty)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UseBriefInstructions)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UseBriefInstructions)
@Html.ValidationMessageFor(model => model.UseBriefInstructions)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@model FaceToFace.Model.User
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>User</legend>
<div class="editor-label">
@Html.LabelFor(model => model.CodeName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CodeName)
@Html.ValidationMessageFor(model => model.CodeName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Device.Name, "Device")
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.User.Device.DeviceID, Model.Devices)
<input type="submit" value="Save" />
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UseBriefInstructions)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UseBriefInstructions)
@Html.ValidationMessageFor(model => model.UseBriefInstructions)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
查看/Patient/Create.cshtml
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FaceToFace.Model
{
public class User
{
public int UserID { get; set; }
public string CodeName { get; set; }
public bool UseBriefInstructions { get; set; }
public Device Device { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FaceToFace.Model
{
public class Device
{
public int DeviceID { get; set; }
public string Name { get; set; }
}
}
@model IEnumerable<FaceToFace.Model.User>
@{
ViewBag.Title = "Index";
}
<h2>Your Patients</h2>
Showing @Model.Count() users
<p>
@Html.ActionLink("Add New User", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.UserID)
</th>
<th>
@Html.DisplayNameFor(model => model.CodeName)
</th>
<th>
@*@Html.DisplayNameFor(model => model.Device.Name)*@Device
</th>
<th>
@Html.DisplayNameFor(model => model.Device.DeviceID)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserID)
</td>
<td>
@Html.DisplayFor(modelItem => item.CodeName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Device.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Device.DeviceID)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.UserID }) |
@Html.ActionLink("Details", "Details", new { id = item.UserID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.UserID })
</td>
</tr>
}
</table>
@model FaceToFace.Model.User
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>User</legend>
<div class="editor-label">
@Html.LabelFor(model => model.CodeName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CodeName)
@Html.ValidationMessageFor(model => model.CodeName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Device.Name, "Device")
</div>
<div class="editor-field">
@Html.DropDownList("DeviceID", String.Empty)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UseBriefInstructions)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UseBriefInstructions)
@Html.ValidationMessageFor(model => model.UseBriefInstructions)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@model FaceToFace.Model.User
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>User</legend>
@Html.HiddenFor(model => model.UserID)
<div class="editor-label">
@Html.LabelFor(model => model.CodeName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CodeName)
@Html.ValidationMessageFor(model => model.CodeName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Device.Name, "Device")
</div>
<div class="editor-field">
@Html.DropDownList("DeviceID", String.Empty)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UseBriefInstructions)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UseBriefInstructions)
@Html.ValidationMessageFor(model => model.UseBriefInstructions)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@model FaceToFace.Model.User
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>User</legend>
<div class="editor-label">
@Html.LabelFor(model => model.CodeName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CodeName)
@Html.ValidationMessageFor(model => model.CodeName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Device.Name, "Device")
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.User.Device.DeviceID, Model.Devices)
<input type="submit" value="Save" />
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UseBriefInstructions)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UseBriefInstructions)
@Html.ValidationMessageFor(model => model.UseBriefInstructions)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@model FaceToFace.model.User
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm()){
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
使用者
@LabelFor(model=>model.CodeName)
@EditorFor(model=>model.CodeName)
@Html.ValidationMessageFor(model=>model.CodeName)
@LabelFor(model=>model.Device.Name,“设备”)
@DropDownListFor(model=>model.User.Device.DeviceID,model.Devices)
@LabelFor(model=>model.UseBriefInstructions)
@EditorFor(model=>model.UseBriefInstructions)
@Html.ValidationMessageFor(model=>model.UseBriefInstructions)
}
@ActionLink(“返回列表”、“索引”)
我将省略编辑,以避免进一步的垃圾邮件,但我现在得到多个错误。我相信我已经解决了一些问题。对于下拉列表,我得到以下错误:
“System.Web.Mvc.HtmlHelper”不包含“DropDownListFor”的定义,而“System.Web.Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper,System.Linq.Expressions.Expression>,System.Collections.Generic.IEnumerable)”的最佳扩展方法重载包含一些无效参数
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using FaceToFace.Model;
using System.Collections;
using System.ComponentModel.DataAnnotations;
namespace FaceToFaceWebsite.Controllers
{
public class PatientController : Controller
{
private F2FData db = new F2FData();
//
// GET: /Patient/
public ActionResult Index()
{
return View(db.Users.ToList());
}
//
// GET: /Patient/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
User user = db.Users.Find(id);
if (user == null)
{
return HttpNotFound();
}
return View(user);
}
// GET: /Patient/Create
public ActionResult Create()
{
ViewBag.DeviceID = new SelectList(db.Devices, "DeviceID", "Name");
return View();
}
//
// POST: /Patient/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "UserID,CodeName,UseBriefInstructions")] User user, Device device)
{
if (ModelState.IsValid)
{
//db.Devices = user.Device.DeviceID;
db.Users.Add(user);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.DeviceID = new SelectList(db.Devices, "DeviceID", "Name", user.Device.DeviceID);
return View(user);
}
//
// GET: /Patient/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
User user = db.Users.Find(id);
if (user == null)
{
return HttpNotFound();
}
//removed user.Device.DeviceID
ViewBag.DeviceID = new SelectList(db.Devices, "DeviceID", "Name");
return View(user);
}
//
// POST: /Patient/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "UserID,CodeName,UseBriefInstructions")] User user)
{
if (ModelState.IsValid)
{
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.DeviceID = new SelectList(db.Devices, "DeviceID", "Name", user.Device.DeviceID);
return View(user);
}
//
// GET: /Patient/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
User user = db.Users.Find(id);
if (user == null)
{
return HttpNotFound();
}
return View(user);
}
//
// POST: /Patient/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
User user = db.Users.Find(id);
db.Users.Remove(user);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.WebPages.Html;
using FaceToFace.Model;
namespace FaceToFaceWebsite.Models
{
public class UserDeviceViewModel
{
public UserDeviceViewModel()
{
User = new User();
Users = new List<User>();
Devices = new List<SelectListItem>();
}
public User UseBriefInstructions { get; set; }
public Device Device { get; set; }
public User User { get; set; }
public User CodeName { get; set; }
public IList<SelectListItem> Devices { get; set; }
public IList<User> Users { get; set; }
}
}
namespace FaceToFaceWebsite.Controllers
{
public class PatientController : Controller
{
private F2FData db = new F2FData();
//
// GET: /Patient/
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(UserDeviceViewModel viewModelDevice)
{
var viewModel = new UserDeviceViewModel();
viewModel.Devices.Add(new SelectListItem() { Text = "MAU110-10", Value = "1" });
viewModel.Devices.Add(new SelectListItem() { Text = "MAU110-100", Value = "2" });
viewModel.Devices.Add(new SelectListItem() { Text = "MAU110-101", Value = "3" });
viewModel.Devices.Add(new SelectListItem() { Text = "MAU110-102", Value = "4" });
viewModel.Devices.Add(new SelectListItem() { Text = "MAU110-103", Value = "5" });
viewModel.Users.AddRange(db.Users.ToList());
return View(viewModel);
}
对于我获得的viewModel.Devices.Add
患者控制器:
与“System.Collections.Generic.ICollection.Add(System.Web.WebPages.Html.SelectListItem)”匹配的最佳重载方法有一些无效参数
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using FaceToFace.Model;
using System.Collections;
using System.ComponentModel.DataAnnotations;
namespace FaceToFaceWebsite.Controllers
{
public class PatientController : Controller
{
private F2FData db = new F2FData();
//
// GET: /Patient/
public ActionResult Index()
{
return View(db.Users.ToList());
}
//
// GET: /Patient/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
User user = db.Users.Find(id);
if (user == null)
{
return HttpNotFound();
}
return View(user);
}
// GET: /Patient/Create
public ActionResult Create()
{
ViewBag.DeviceID = new SelectList(db.Devices, "DeviceID", "Name");
return View();
}
//
// POST: /Patient/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "UserID,CodeName,UseBriefInstructions")] User user, Device device)
{
if (ModelState.IsValid)
{
//db.Devices = user.Device.DeviceID;
db.Users.Add(user);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.DeviceID = new SelectList(db.Devices, "DeviceID", "Name", user.Device.DeviceID);
return View(user);
}
//
// GET: /Patient/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
User user = db.Users.Find(id);
if (user == null)
{
return HttpNotFound();
}
//removed user.Device.DeviceID
ViewBag.DeviceID = new SelectList(db.Devices, "DeviceID", "Name");
return View(user);
}
//
// POST: /Patient/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "UserID,CodeName,UseBriefInstructions")] User user)
{
if (ModelState.IsValid)
{
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.DeviceID = new SelectList(db.Devices, "DeviceID", "Name", user.Device.DeviceID);
return View(user);
}
//
// GET: /Patient/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
User user = db.Users.Find(id);
if (user == null)
{
return HttpNotFound();
}
return View(user);
}
//
// POST: /Patient/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
User user = db.Users.Find(id);
db.Users.Remove(user);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.WebPages.Html;
using FaceToFace.Model;
namespace FaceToFaceWebsite.Models
{
public class UserDeviceViewModel
{
public UserDeviceViewModel()
{
User = new User();
Users = new List<User>();
Devices = new List<SelectListItem>();
}
public User UseBriefInstructions { get; set; }
public Device Device { get; set; }
public User User { get; set; }
public User CodeName { get; set; }
public IList<SelectListItem> Devices { get; set; }
public IList<User> Users { get; set; }
}
}
namespace FaceToFaceWebsite.Controllers
{
public class PatientController : Controller
{
private F2FData db = new F2FData();
//
// GET: /Patient/
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(UserDeviceViewModel viewModelDevice)
{
var viewModel = new UserDeviceViewModel();
viewModel.Devices.Add(new SelectListItem() { Text = "MAU110-10", Value = "1" });
viewModel.Devices.Add(new SelectListItem() { Text = "MAU110-100", Value = "2" });
viewModel.Devices.Add(new SelectListItem() { Text = "MAU110-101", Value = "3" });
viewModel.Devices.Add(new SelectListItem() { Text = "MAU110-102", Value = "4" });
viewModel.Devices.Add(new SelectListItem() { Text = "MAU110-103", Value = "5" });
viewModel.Users.AddRange(db.Users.ToList());
return View(viewModel);
}
以及:
参数1:无法从“System.Web.Mvc.SelectListItem”转换为“System.Web.WebPages.Html.SelectListItem”
最后,我从AddRange收到一个错误:
“System.Collections.Generic.IList”不包含“AddRange”的定义,并且找不到接受“System.Collections.Generic.IList”类型的第一个参数的扩展方法“AddRange”(是否缺少using指令或程序集引用?
我们将非常感谢您的帮助。再次为垃圾邮件道歉 看起来您没有为视图提供可选择的设备 不过,总的来说,我建议您在这里使用viewmodel方法。e、 g 用户
public class User
{
public User()
{
Device = new Device();
}
public int UserID { get; set; }
public string CodeName { get; set; }
public bool UseBriefInstructions { get; set; }
public Device Device { get; set; }
}
设备
public class Device
{
public int DeviceID { get; set; }
public string Name { get; set; }
}
然后创建一个viewmodel类:
public class CreatePatientViewModel
{
public CreatePatientViewModel()
{
User = new User();
Devices = new List<SelectListItem>();
}
public User User { get; set; }
public IList<SelectListItem> Devices { get; set; }
}
然后可以使用.Devices
属性填充视图中的下拉列表,例如
@model StackOverflowMvc.Controllers.CreatePatientViewModel
@using (Html.BeginForm())
{
@Html.DropDownListFor(model => model.User.Device.DeviceID, Model.Devices)
<input type="submit" value="Save" />
}
不要为[Bind(Include=“UserID,code…”
之类的东西操心,它只会让事情变得混乱。CreatePatientViewModel
可用于从提交的表单中提取值,例如
@Html.InputFor(model=>model.User.code)
在视图中,如果上面的文本框中输入了一个值,那么当执行POST
时,CreatePatientViewModel.User.code
将传递该值,因为它与使用的model.User.code
匹配
希望以上内容对您有所帮助
编辑:
在控制器的索引
操作方法中,您可以执行以下操作:
public ActionResult Index()
{
var viewModel = new CreatePatientViewModel();
viewModel.Devices.Add(new SelectListItem() { Text = "Device 1", Value = "1" });
viewModel.Devices.Add(new SelectListItem() { Text = "Device 2", Value = "2" });
viewModel.Users.AddRange(db.Users.ToList());
return View(viewModel);
}
更改CreatePatientViewModel
以包含Users
属性:
public class CreatePatientViewModel
{
public CreatePatientViewModel()
{
User = new User();
Users = new List<User>();
Devices = new List<SelectListItem>();
}
public User User { get; set; }
public IList<SelectListItem> Devices { get; set; }
public IList<User> Users { get; set; }
}
public类CreatePatientViewModel
{
公共CreatePatientViewModel()
{
User=新用户();
用户=新列表();
设备=新列表();
}
公共用户{get;set;}
公共IList设备{get;set;}
公共IList用户{get;set;}
}
请注意,我假设您的db.Users列表是一个
User
对象列表,如果不是,那么只需将其更改为任何类型即可。Thankyou Jason!在控制器中,它位于公共ActionResult索引下。这是要替换当前的索引ActionResult,我可以在它上面再做一个,还是将它放在帖子中作为t他创建了ActionResult?再次感谢您的帮助,非常感谢。我无法让它正常工作,因为我不确定应该将您提供的信息放在PatientController中的何处。再次感谢您,我对viewModels没有太多经验,它告诉我,我的新viewmodel没有包含Cod的定义eName(用户模型的一部分)我该如何补救?我还没有得到“AddRange”的定义,也不太清楚为什么会发生这种情况。我不确定这是否会产生影响,但用户模型和设备是我在解决方案中引入的另一个项目的一部分。