这是我的模特
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace TheFoody.Models
{
public class ManageViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Photo { get; set; }
public string Address { get; set; }
public string City { get; set; }
public int PostCode { get; set; }
public string District { get; set; }
public string UserType { get; set; }
public string Status { get; set; }
}
}
这是我的上下文
namespace TheFoody.DataAccess
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class TheFoodyContext : DbContext
{
public TheFoodyContext()
: base("name=TheFoodyContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Menu> Menus { get; set; }
public virtual DbSet<User> Users { get; set; }
public virtual DbSet<Category> Categories { get; set; }
public virtual DbSet<Restaurant> Restaurants { get; set; }
public virtual DbSet<Restaurant_Type> Restaurant_Type { get; set; }
}
}
这是我的控制器
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TheFoody.DataAccess;
using TheFoody.Models;
namespace TheFoody.Controllers
{
public class ManageController : Controller
{
public ActionResult Manage()
{
return View();
}
[HttpPost]
public ActionResult Manage(ManageViewModel manageviewmodel)
{
TheFoodyContext db = new TheFoodyContext();
ManageViewModel user_to_update = db.Users.SingleOrDefault(s => s.email == manageviewmodel.Email);
return View(manageviewmodel);
}
}
}
但是在这里我正在尝试编写这部分代码“ ManageViewModel user_to_update = db.Users.SingleOrDefault(s =&gt; s.email == manageviewmodel.Email); ”它会给我突出显示错误名为“无法将类型'TheFoody.DataAccess.User'隐式转换为'TheFoody.Models.ManageViewModel'”
我对这个环境很新,我不知道为什么会出现这样的错误。
答案 0 :(得分:5)
=INDEX(Sheet1!A:A,MATCH(A2,Sheet2!A:A,0))
=INDEX(Sheet1!A:A,MATCH("*"&A2&"*",Sheet1!A:A,0))
是db.Users
,DbSet<TheFoody.DataAccess.User>
返回一个此类用户,如果该电子邮件地址未找到任何用户,则返回null。
该类的实例无法隐式转换为请求的SingleOrDefault()
,您需要映射它:
ManageViewModel
您可以使用AutoMapper以更方便的方式进行映射。
虽然,您似乎想要使用通过POST发送的模型更新数据库,但您需要更改逻辑:您需要从收到的{DataAccess.User user = db.Users.SingleOrDefault(...);
if (user == null)
{
// show error page telling that the POSTed email address is not known
}
ManageViewModel user_to_update = new ManageViewModel
{
FirstName = user.FirstName,
LastName = user.LastName,
// ...
}
return View(user_to_update);
中分配user
属性。 1}}:
manageviewmodel
答案 1 :(得分:2)
那是因为您无法直接将用户类型转换为 ManageViewModel
您可以通过将查询结果分配给用户类型然后重复用户类型的属性并将其分配给相关属性来解决问题。 ManageViewModel 类型。
示例:
User user_to_update = db.Users.SingleOrDefault(s => s.email == manageviewmodel.Email);
if (user != null)
{
var manageViewModel = new ManageViewModel()
manageViewModel.FirstName = user.FirstName;
manageViewModel.LastName = user.LastName;
}
else {
//Manage null exception here
}
......等等