【发布时间】:2017-11-22 14:37:22
【问题描述】:
返回并将所需值传递给视图时遇到问题。我在 LINQPad4 中运行 LINQ 查询,它返回正确的结果。我觉得我在这里遗漏了一些简单的东西。我用这篇文章HERE 作为参考
我收到了
CS1061:“IEnumerable”不包含“FirstName”的定义,并且找不到接受“IEnumerable”类型的第一个参数的扩展方法“FirstName”(您是否缺少 using 指令或程序集引用?)错误. \
当我运行并单步执行代码时,我没有看到任何值,我得到的消息是
Message = "无法创建 'System.Object' 类型的常量值。在此上下文中仅支持原始类型或枚举类型。"
更新
将视图从 @model IEnumerable<Login.Models.EditProfile> 更改为 @model Login.Models.EditProfile 帮助解决了我遇到的 CS1061 错误,至于 LINQ 查询不起作用,请查看以下 Titian 接受的答案。
任何帮助将不胜感激。 如果我可以提供更多信息,请告诉我。
/ 控制器 /
public ActionResult Edit(int? id)
{
using (TDBEntities db1 = new TDBEntities())
{
var user = (from a in db1.ExternalUsers
join b in db1.ExternalUserEmails on a.ExternalUserID equals b.ExternalUserID
join c in db1.ExternalUserPhones on a.ExternalUserID equals c.ExternalUserID
where a.ExternalUserID.Equals(id)
select new EditProfile {ExternalUserID = a.ExternalUserID, FirstName = a.FirstName, LastName = a.LastName, EmailAddress = b.EmailAddress, PhoneNumber = c.PhoneNumber });
if (user == null)
{
return HttpNotFound();
}
return View(user);
}
}
/型号/
public partial class EditProfile
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
public string PhoneNumber { get; set; }
public int ExternalUserID { get; set; }
}
/查看/
@model IEnumerable<Login.Models.EditProfile>
@using Login.Helpers
@{
ViewBag.Title = "Update Employee";
}
<h2></h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Update Employee</h4>
<hr />
<div class="form-group">
@Html.LabelFor(model => model.FirstName, "First Name:", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, "Last Name:", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EmailAddress, "Email Address:", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EmailAddress, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EmailAddress, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PhoneNumber, "Phone Number:", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PhoneNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PhoneNumber, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<label class="resetPw control-label col-md-2 ">Reset Password</label>
<div> @Html.CheckBox("ResetPassword", false, new { @style = "margin: 10px 15px 0;" }) <i>(check to reset)</i></div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
【问题讨论】:
-
如果您正在编辑单个记录,您的视图应该强类型为
@model Login.Models.EditProfile。此外,您需要从操作方法发送单个对象来查看,而不是集合。使用FirstOrDefault()
标签: c# asp.net-mvc entity-framework linq