Vit*_*ugo 4 c# asp.net asp.net-mvc razor asp.net-core
我在做什么?
我正在尝试在一个视图中使用两个模态。
有什么问题?
运行时出现这个错误:
An unhandled exception occurred while processing the request.
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'ProgramaEstagio.Models.Person', but this ViewDataDictionary instance requires a model item of type 'ProgramaEstagio.Models.RegisterViewModel'.
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary.EnsureCompatible(object value)
Run Code Online (Sandbox Code Playgroud)
“PersonController.cs”的一部分:
An unhandled exception occurred while processing the request.
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'ProgramaEstagio.Models.Person', but this ViewDataDictionary instance requires a model item of type 'ProgramaEstagio.Models.RegisterViewModel'.
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary.EnsureCompatible(object value)
Run Code Online (Sandbox Code Playgroud)
注册ViewModel.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using ProgramaEstagio.Data;
using ProgramaEstagio.Models;
namespace ProgramaEstagio.Controllers
{
public class PersonController : Controller
{
private readonly ApplicationDbContext _context;
public PersonController(ApplicationDbContext context)
{
_context = context;
}
// GET: Person
public async Task<IActionResult> Index()
{
return View(await _context.Person.ToListAsync());
}
// GET: Person/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var person = await _context.Person.FindAsync(id);
if (person == null)
{
return NotFound();
}
return View(person);
}
// POST: Person/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("ID,FullName,BirthDate,sex")] Person person)
{
if (id != person.ID)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(person);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!PersonExists(person.ID))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(person);
}
Run Code Online (Sandbox Code Playgroud)
编辑.cshtml:
namespace ProgramaEstagio.Models
{
public class RegisterViewModel
{
public Person Person { get; set; }
public Address Address { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
我必须做什么?请帮我。如果需要更多信息,请询问。对于任何拼写和语法错误,我深表歉意,因为我正在学习英语。项目链接:https://github.com/vitorhugo1207/Sys-Vacina/tree/EditPagePersonAdress
编辑: AddressController.cs:
@model ProgramaEstagio.Models.RegisterViewModel
@{
ViewData["Title"] = "Edit";
}
<h1>Edit</h1>
<h4>Person</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="Person.ID" />
<div class="form-group">
<label asp-for="Person.FullName" class="control-label"></label>
<input asp-for="Person.FullName" class="form-control" />
<span asp-validation-for="Person.FullName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Person.BirthDate" class="control-label"></label>
<input asp-for="Person.BirthDate" class="form-control" />
<span asp-validation-for="Person.BirthDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Person.sex" class="control-label"></label>
<input asp-for="Person.sex" class="form-control" />
<span asp-validation-for="Person.sex" class="text-danger"></span>
</div>
<h4>Address</h4>
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="Address.ID" />
<div class="form-group">
<label asp-for="Address.Country" class="control-label"></label>
<input asp-for="Address.Country" class="form-control" />
<span asp-validation-for="Address.Country" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Address.Complement" class="control-label"></label>
<input asp-for="Address.Complement" class="form-control" />
<span asp-validation-for="Address.Complement" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Address.Distric" class="control-label"></label>
<input asp-for="Address.Distric" class="form-control" />
<span asp-validation-for="Address.Distric" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Address.City" class="control-label"></label>
<input asp-for="Address.City" class="form-control" />
<span asp-validation-for="Address.City" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Address.State" class="control-label"></label>
<input asp-for="Address.State" class="form-control" />
<span asp-validation-for="Address.State" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Address.PersonID" class="control-label"></label>
<select asp-for="Address.PersonID" class="form-control" asp-items="ViewBag.PersonID"></select>
<span asp-validation-for="Address.PersonID" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Run Code Online (Sandbox Code Playgroud)
它发生在这条线上:return View(person);。您正在发送ModelPerson 类型,但需要发送ModelRegisterViewModel 类型。您需要将正确的信息发送Model至您的View:
public async Task<IActionResult> Edit(int? id)
{
RegisterViewModel register = new RegisterViewModel();
if (id == null)
{
return NotFound();
}
var person = await _context.Person.FindAsync(id);
if (person == null)
{
return NotFound();
}
register.Person = person;
var address = await _context.Address.FindAsync(id);
if (address == null)
{
return NotFound();
}
register.Address = address;
return View(register);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9537 次 |
| 最近记录: |