我在尝试将我的UserID和DeviceID(两个独立模型的一部分)显示在我的一个Index视图上时遇到了很多麻烦。我建立一个网站,允许通过创建用户(AKA患者),您可以添加可与该用户关联的设备。我通过下拉种子设备列表来做到这一点。虽然下拉列表显示了设备,但它不会使用所选设备更新索引。
我的目标是获得DeviceID&在我的用户(AKA患者)索引视图上正确显示的名称。此外,当我创建和编辑用户时,我希望下拉菜单更新患者(用户)索引视图上的用户信息。
PatientController.cs
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);
}
}
}
User.cs(型号)
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; }
}
}
Device.cs(型号)
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; }
}
}
查看/患者/ Index.cshtml
@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>
查看/患者/ Create.cshtml
@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>
查看/患者/ Edit.cshtml
@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>
谢谢您花时间阅读本文,我是MVC的新手,所以我感谢您提供的任何帮助。
更新
在杰森的帮助下,我开始构建一个视图模型。我在我的viewmodel中使用的模型是我加入了我的解决方案的另一个项目。
UserDeviceViewModel.cs
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; }
}
}
PatientController.cs
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);
}
查看/患者/ Create.cshtml
@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>
我会省略编辑以避免进一步的垃圾邮件,但我现在收到多个错误。我已经修好了几个我相信的。对于下拉列表,我收到此错误:
&#39; System.Web.Mvc.HtmlHelper&#39;不包含&#39; DropDownListFor&#39;的定义和最好的扩展方法重载&#39; System.Web.Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper,System.Linq.Expressions.Expression&gt;,System.Collections.Generic.IEnumerable)&#39 ;有一些无效的论点
对于我正在获得的患者控制器中的viewModel.Devices.Add
:
最佳重载方法匹配&#39; System.Collections.Generic.ICollection.Add(System.Web.WebPages.Html.SelectListItem)&#39;有一些无效的论点
以及:
参数1:无法转换为System.Web.Mvc.SelectListItem&#39;到&#39; System.Web.WebPages.Html.SelectListItem&#39;
最后,我收到了AddRange的错误:
&#39; System.Collections.Generic.IList&#39;不包含&#39; AddRange&#39;的定义没有扩展方法&#39; AddRange&#39;接受类型为'System.Collections.Generic.IList&#39;的第一个参数。可以找到(你错过了使用指令或汇编引用吗?)
任何有助于实现这一目标的帮助将不胜感激。再次,抱歉垃圾邮件!
答案 0 :(得分:2)
您似乎无法通过一系列可供选择的设备提供视图。
总的来说,我建议你在这里使用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; }
}
在控制器中,您可以像这样使用viewmode -
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" });
return View(viewModel);
}
然后可以使用.Devices
属性填充视图中的下拉列表,例如
@model StackOverflowMvc.Controllers.CreatePatientViewModel
@using (Html.BeginForm())
{
@Html.DropDownListFor(model => model.User.Device.DeviceID, Model.Devices)
<input type="submit" value="Save" />
}
提交数据时:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(CreatePatientViewModel viewModel)
{
// Do whatever you want with the data.
// viewModel.User.Device.DeviceId will be given the value selected from the dropdown.
return View(viewModel);
}
不要理会[Bind(Include = "UserID,Code....
的东西,这只会让事情变得混乱。 CreatePatientViewModel
可用于从提交的表单中获取值,例如
@Html.InputFor(model => model.User.Code)
在视图中,如果上面的文本框输入了一个值,那么当POST
成为CreatePatientViewModel.User.Code
时,model.User.Code
将传递该值,因为它与使用的Index
匹配。 / p>
希望上面的内容对你有帮助。
编辑:
在控制器的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; }
}
属性:
User
请注意,我假设您的db.Users列表是{{1}}个对象的列表,如果不是,那么只需将其更改为它应该的任何类型。