C# 我想用一张表格填写多个表格

C# 我想用一张表格填写多个表格,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我以前检查过很多其他线程,但没有找到与我的问题相关的任何特定内容,如下所示: 我正在与应用程序用户一起完成我的项目。除此之外,每个用户都有一个配置文件(这是一个不同的表)。我想在后台“创建”新的ApplicationUser时填写配置文件,因为它们是通过用户ID相互连接的。问题是,这不太管用。我使用的是ViewModels,我还为ApplicationUsers制作了一个特定的Viewmodel(UserViewModel) 到目前为止,我完全可以创建一个新的ApplicationUser,但是

我以前检查过很多其他线程,但没有找到与我的问题相关的任何特定内容,如下所示:

我正在与应用程序用户一起完成我的项目。除此之外,每个用户都有一个配置文件(这是一个不同的表)。我想在后台“创建”新的ApplicationUser时填写配置文件,因为它们是通过用户ID相互连接的。问题是,这不太管用。我使用的是ViewModels,我还为ApplicationUsers制作了一个特定的Viewmodel(UserViewModel)

到目前为止,我完全可以创建一个新的ApplicationUser,但是一旦我开始尝试使用与我用于ApplicationUser的表单相同的表单创建概要文件,事情就开始出错。我有一些错误的预感(例如,在我的视图中仅使用1个模型(create/edit.cshtml,尽管我很确定在视图中创建时只能使用1个模型)

下面是我的用户视图模型(UserViewModel.cs) 正如您在这里看到的,我的UserViewModel有一个虚拟财产配置文件,它应该能够与用户一起创建配置文件?或者可能我已经大错特错了

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using App.Models;
using App.Models.ViewModels;

namespace App.Models.Identity.ViewModels
{
    public class UserViewModel
    {
        public string UserId { get; set; }
        public string UserName { get; set; }
        public string Email { get; set; }  
        public virtual Profile Profile { get; set; }
    }
}
以下是我的编辑函数(UserController.cs) 请记住,当我删除对配置文件的任何引用时,一切都已经基本正常。当我开始尝试添加配置文件字段(无论是在这里还是在下面的视图中)时,问题开始出现

        [HttpGet]
        public IActionResult Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(400);
            }

            var model = _kletsContext.Users.FirstOrDefault(m => m.Id == id);
            var profile = _kletsContext.Profiles.FirstOrDefault(m => m.UserId == model.Id);
            if(model == null)
            {
                return RedirectToAction("Index");
            }

            var viewModel = new UserViewModel
            {
                UserId = model.Id,
                UserName = model.UserName,
                Email = model.Email,
            };

            return View(viewModel);
        }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult Edit(UserViewModel model, Profile profile)
    {
        UserViewModel viewModel = null;

        try 
        {
            if(!ModelState.IsValid)
                throw new Exception("The User model is not valid!");

            var originalModel = _kletsContext.Users.FirstOrDefault(m => m.Id == model.UserId);
            var originalProfile = _kletsContext.Profiles.FirstOrDefault(m => m.UserId == model.UserId);

            if(originalModel == null)
                throw new Exception("The existing User: " + model.UserName + " doesn't exists anymore!");

            originalModel.UserName = model.UserName;
            originalModel.Email = model.Email;
            originalProfile.Age = profile.Age;


            _kletsContext.Users.Attach(originalModel);
            _kletsContext.Profiles.Attach(profile);
            _kletsContext.Entry(originalModel).State = EntityState.Modified;
            _kletsContext.Entry(profile).State = EntityState.Modified;

            if (_kletsContext.SaveChanges() == 0)
            {
               throw new Exception("The User model could not be saved!");
            }

            return RedirectToAction("Index");

        }
        catch(Exception ex)
        {
            ModelState.AddModelError(string.Empty, "Unable to save changes.");

            viewModel = new UserViewModel
            {
                UserId = model.UserId,
                UserName = model.UserName,
                Email = model.Email,

            };    
        }
        return View(viewModel);
    }
下面是my Edit.cshtml:
@model App.Models.Identity.ViewModels.UserViewModel
@{
ViewBag.Title=“用户”;
ViewBag.SubTitle=“Nieuwe”;
ViewBag.Description=“Aanmaak van een nieuwe User”;
Layout=“~/Areas/Backoffice/Views/Shared/_Layout.cshtml”;
}
安马克·范恩·尼乌用户
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(m=>m.UserId)
安马克·范恩·尼乌用户
@Html.ValidationSummary(“,new{@class=“alert danger”})
@LabelFor(m=>m.UserName)
@TextBoxFor(m=>m.UserName,新的{@class=“form control”})
@LabelFor(m=>m.Email)
@TextBoxFor(m=>m.Email,新的{@class=“form control”})
@LabelFor(m=>m.Profile.Age)
@TextBoxFor(m=>m.Profile.Age,新的{@class=“form control”})
@ActionLink(“Terug-naar-het-overzicht”,“Index”,new{},new{@class=“btn-btn-default”})
}
额外:如果需要,我会添加我的ProfileViewModel,或者我的DBContext文件(或任何模型)。请告诉我。我已经看了一段时间了,但我很确定我只是误解了一些非常基本的东西

PS:我最终得到了exceptionError,所以我知道这是一个一般性的问题,我的Try中没有任何东西起作用。请参见下图


您应该尝试使用倾斜平面视图模型

public class UserProfileViewModel
{
    public string UserId { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }  
    public int Age { get; set; }
}
在你的行动中

public ActionResult Edit(string id)
{

   var model = _kletsContext.Users.FirstOrDefault(m => m.Id == id);
   var profile = _kletsContext.Profiles.FirstOrDefault(m => m.UserId == model.Id);
   if(model == null)
   {
       return RedirectToAction("Index");
   }

   var vm = new UserProfileViewModel
   {
       UserId = model.Id,
       UserName = model.UserName,
       Email = model.Email,
       Age = profile.Age
   };
   return View(vm);
}
您的razor视图将强类型化到此视图模型

@model YourNamespaceHere.UserProfileViewModel
@using(Html.BeginForm())
{
  <label>UserName</label>
  @Html.TextBoxFor(s=>s.UserName)

  <label>Email</label>
  @Html.TextBoxFor(s=>s.Email)

  <label>Age</label>
  @Html.TextBoxFor(s=>s.Age)
  @Html.HiddenFor(s=>s.UserId)
  <input type="submit" />
}

您可以设置断点并查看异常的实际含义吗?另外,您可以检查
originalModel
是否为null,但不检查
originalProfile
。您确定它不为null吗?例如,在控制器中当前错误消息所在的行上设置断点?到目前为止,我还没有真正使用断点(我也是Mac用户,也使用Visual Studio代码)我已将originalProfile添加到if语句中(检查它是否也不为null)。它仍然会被捕获,所以可以肯定地说它不是空的。我已经将它某种程度上纳入了我自己的逻辑。它保存了更改,以便编辑工作!唯一的问题是,尽管这样做,它仍然会将我发送到我的异常,这让我觉得有点奇怪。我会尝试进一步解决这个问题。不过,非常感谢!
@model YourNamespaceHere.UserProfileViewModel
@using(Html.BeginForm())
{
  <label>UserName</label>
  @Html.TextBoxFor(s=>s.UserName)

  <label>Email</label>
  @Html.TextBoxFor(s=>s.Email)

  <label>Age</label>
  @Html.TextBoxFor(s=>s.Age)
  @Html.HiddenFor(s=>s.UserId)
  <input type="submit" />
}
[HttpPost]
public ActionResult Edit(UserProfileViewModel model)
{
  if(ModelState.IsValid)
  {
     var u = _kletsContext.Users.FirstOrDefault(m => m.Id == model.UserId);
     var p= _kletsContext.Profiles.FirstOrDefault(m => m.UserId == model.UserId);

     //Set the new values
     u.Email = model.Email;

     if(p!=null)
     {
        p.Age=model.Age;
        _kletsContext.Entry(p).State = EntityState.Modified;
     }
     else
     {
        // to do  :Create a new profile record
     }
     _kletsContext.Entry(u).State = EntityState.Modified;

    _kletsContext.SaveChanges();
     // to redirect to some success page
  }
  return View(model);
}