gpt4 book ai didi

c# - 如何解决 ASP.NET C# 中的类型转换错误?

转载 作者:行者123 更新时间:2023-11-30 19:54:57 25 4
gpt4 key购买 nike

这是我的模型

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; }
}
}

这是我的 Controller

 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 => s.email == manageviewmodel.Email);”它会给我突出显示名为“无法将类型‘TheFoody.DataAccess.User’隐式转换为‘TheFoody.Models.ManageViewModel’”的错误

我对这个环境很陌生,我不知道为什么会出现这样的错误。

最佳答案

db.UsersDbSet<TheFoody.DataAccess.User> , 和 SingleOrDefault()返回一个这样的用户,如果该电子邮件地址未找到任何用户,则返回 null。

该类的实例不能隐式转换为请求的 ManageViewModel ,你需要映射它:

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);

您可以使用AutoMapper 以更方便的方式进行映射。

虽然,您似乎想用通过 POST 发送的模型更新数据库,然后您需要更改逻辑:您需要分配 user收到的属性 manageviewmodel :

// read from database
DataAccess.User user = db.Users.SingleOrDefault(...);

if (user == null)
{
// show error page telling that the POSTed email address is not known
}

// update from POST
user.FirstName = manageviewmodel.FirstName;
user.LastName = manageviewmodel.LastName;
// ...

// persist to database
db.SaveChanges();

关于c# - 如何解决 ASP.NET C# 中的类型转换错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40147940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com