繁体   English   中英

检索和填充数据 ASP.Net Core 2.2 MVC

[英]Retrieve and populate data ASP.Net Core 2.2 MVC

我的控制器中有以下代码,我将返回一个帐户视图,将参数传递给它,如下所示

[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'.

我的主要问题是如何使用检索到的数据填充到输入字段中? 我感谢任何指导/帮助。

改变

var contactDetails =
          (from c in _context.UserPersonalDetails
           where c.ID == findUser.Id
           select c);

if( findUser ==null) return NotFound("user is not found");

var contactDetails =
          (from c in _context.UserPersonalDetails
           where c.ID == findUser.Id
           select c).FirstOrDefault();
or even shorter:
var contactDetails = await _context.DbSet<UserPersonalDetails>()
           .Where(c=> c.ID == findUser.Id)
           .FirstOrDefaultAsync();
And add  attribut  [Table("UserPersonalDetails")] to the top of the class.

Just remember for the future: NEVER GIVE PLURAL NAMES ENDING AT S TO ENTITY. It is very confusing since EF usually almost automaticly assingns plural names to the collections.

查询返回iQueryable<UserPersonalDetails>您的模型等待UserPersonalDetails在控制器中尝试此编辑

var contactDetails =(from c in _context.UserPersonalDetails where c.ID == findUser.Id select c).FirstOrDefault() ;

SqlNullValueException: 数据为 Null。 不能对 Null 值调用此方法或属性。

似乎表列具有NULL值,而您的所有属性都设置为[Required]属性,因此会引发此错误。 您可以检查UserPersonalDetails表中 ID 等于findUser.Id的记录,以查找其列中是否findUser.Id值。

一个类似的问题在这里解决 你可以参考一下。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM