【发布时间】:2021-02-13 17:40:21
【问题描述】:
我的控制器中有以下代码,我将在其中返回一个帐户视图,并将参数传递给它,如下所示
[HttpGet]
public async Task<ActionResult> Account()
{
var findUser = await UserMgr.FindByNameAsync(User.Identity.Name);
var contactDetails =
(from c in _context.UserPersonalDetails
where c.ID == findUser.Id
select c);
var result = _context.Database.ExecuteSqlCommand("select * from [dbo].[UserPersonalDetails] where ID={0}", findUser.Id);
ViewBag.Data = contactDetails;
return View(contactDetails);
}
这是我的模特
public class UserPersonalDetails
{
[Key]
public int ID { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 3)]
[DataType(DataType.Text)]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 3)]
[DataType(DataType.Text)]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required]
[DataType(DataType.PhoneNumber)]
[Display(Name = "Phone")]
public string Phone { get; set; }}
这是我声明模型的视图,并且
@model FriendShipping.Models.UserPersonalDetails
<form method="post" asp-action="account" asp-controller="account" id="registrationForm">
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<div class="col-xs-6">
<label asp-for="FirstName"></label>
<input asp-for="FirstName" type="text" class="form-control" />
<span asp-validation-for="FirstName" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div class="col-xs-6">
<label asp-for="LastName"></label>
<input asp-for="LastName" type="text" class="form-control" />
<span asp-validation-for="LastName" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div class="col-xs-6">
<label asp-for="Phone"></label>
<input asp-for="Phone" type="text" class="form-control" />
<span asp-validation-for="Phone" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
<br>
<button type="submit" class="btn btn-success small" id="saveUpdatedDetails" disabled><i class="glyphicon glyphicon-ok-sign"></i> Save</button>
<button type="button" class="btn" id="enableUpdate"><i class="glyphicon glyphicon-repeat"></i> Update</button>
</div>
</div>
</form>
返回视图时,我收到以下错误,我尝试通过多种方式重构代码,但它仍然存在
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[FriendShipping.Models.UserPersonalDetails]', but this ViewDataDictionary instance requires a model item of type 'FriendShipping.Models.UserPersonalDetails'.
我的主要问题是如何使用检索到的数据填充到输入字段中? 感谢任何指导/帮助。
【问题讨论】:
标签: c# asp.net-core model-view-controller entity-framework-core data-retrieval