无法在剃须刀页面中保存表单详细信息,如Pluralsight基础知识课程中所示 [英] Unable to save form details in razor page as demonstrated in Pluralsight Fundamentals course

查看:26
本文介绍了无法在剃须刀页面中保存表单详细信息,如Pluralsight基础知识课程中所示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习Scott Allen的Pluralsight的ASP.NET核心基础课程,并且一直在使用我为一个项目创建的自己的程序来完成它。到目前为止,我的代码与视频中的代码非常相似,只是我使用的是我自己的类对象,而不是示例中的Restaurant类。

我遇到的问题是本课程第四个模块中的模型绑定视频。当我运行该程序并在浏览器中编辑一个会议室对象时,它会保存一个空/空对象,而不是使用表单中的输入。我的MeetingSpaceData类(包含更新方法)的单元测试按预期工作,这使我相信我在负责这些编辑的编辑剃须刀页面中犯了一个错误。

编辑:在更新方法(在Edit.cshtml.cs中调用)中接收和使用的MeetingSpace对象是默认的MeetingSpace。因此,问题在于输入表单不用于编辑新创建的对象。

下面是一些相关的内容。如果需要更多的细节,请让我知道。我是一个相当缺乏经验的程序员,也是使用ASP.NET的新手,如果有什么不清楚的地方,请原谅。

MeetingSpaceData类

public class InMemoryMeetingSpaceData : IMeetingSpaceData
{
    public List<MeetingSpace> meetingSpaces;

    public InMemoryMeetingSpaceData()
    {
        meetingSpaces = new List<MeetingSpace>()
        {
            new MeetingSpace { Id = 1, Name = "Meeting Room  1", Location = "The Building", Capacity = 8},
            new MeetingSpace { Id = 2, Name = "Meeting Room 2", Location = "The Building", Capacity = 4},
            new MeetingSpace { Id = 3, Name = "Meeting Room 3", Location = "The Building", Capacity = 6},
            new MeetingSpace { Id = 4, Name = "Meeting Room 4", Location = "The Building", Capacity = 8},
            new MeetingSpace { Id = 5, Name = "Meeting Room 1", Location = "Second Building", Capacity = 7}
        };
    }

    public MeetingSpace Add(MeetingSpace newMeetingSpace)
    {
        if (newMeetingSpace != null)
        {
            meetingSpaces.Add(newMeetingSpace);
            newMeetingSpace.Id = meetingSpaces.Max(m => m.Id) + 1;
        }
        return newMeetingSpace;
    }

    public IEnumerable<MeetingSpace> GetAll()
    {
        return from m in meetingSpaces
               orderby m.Name
               select m;
    }

    public MeetingSpace Update(MeetingSpace updatedMeetingSpace)
    {
        var meetingSpace = meetingSpaces.SingleOrDefault(m => m.Id == updatedMeetingSpace.Id);
        if (meetingSpace != null)
        {
            meetingSpace.Name = updatedMeetingSpace.Name;
            meetingSpace.Location = updatedMeetingSpace.Location;
            meetingSpace.Capacity = updatedMeetingSpace.Capacity;
        }
        return meetingSpace;
    }

    public int Commit()
    {
        return 0;
    }

    public MeetingSpace GetById(int id)
    {
        return meetingSpaces.SingleOrDefault(m => m.Id == id);
    }

}

}

Edit.cshtml

@page "{meetingSpaceId:int}"
@model BookingApp.Pages.MeetingSpaces.EditModel
@{
    ViewData["Title"] = "Edit";
}

<h2>Editing @Model.MeetingSpace.Name</h2>

<form method="post">
    <input type="hidden" asp-for="MeetingSpace.Id" />
    <div class="form-group">
        <label asp-for="MeetingSpace.Name"></label>
        <input asp-for="MeetingSpace.Name" class="form-control" />
        <span class="text-danger" asp-validation-for="MeetingSpace.Name"></span>
    </div>
    <div class="form-group">
        <label asp-for="MeetingSpace.Location"></label>
        <input asp-for="MeetingSpace.Location" class="form-control" />
        <span class="text-danger" asp-validation-for="MeetingSpace.Location"></span>
    </div>
    <div class="form-group">
        <label asp-for="MeetingSpace.Capacity"></label>
        <input asp-for="MeetingSpace.Capacity" class="form-control" />
        <span class="text-danger" asp-validation-for="MeetingSpace.Capacity"></span>
    </div>

    <button type="submit" class="btn btn-primary">Save</button>
</form>

Edit.cshtml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BookingApp.Core;
using BookingApp.Data;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace BookingApp.Pages.MeetingSpaces
{
    public class EditModel : PageModel
    {
        private readonly IMeetingSpaceData meetingSpaceData;

        [BindProperty]
        public MeetingSpace MeetingSpace { get; set; }

        public EditModel(IMeetingSpaceData meetingSpaceData)
        {
            this.meetingSpaceData = meetingSpaceData;
        }

        public IActionResult OnGet(int meetingSpaceId)
        {
            MeetingSpace = meetingSpaceData.GetById(meetingSpaceId);
            if (MeetingSpace == null)
            {
                return RedirectToPage("./NotFound");
            }
            return Page();
        }

        public IActionResult OnPost()
        {
            MeetingSpace = meetingSpaceData.Update(MeetingSpace);
            meetingSpaceData.Commit();
            return Page();
        }
    }
}

MeetingSpace.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BookingApp.Core
{
    public class MeetingSpace : MeetingObjectsBaseClass, IEquatable<MeetingSpace>
    {
        //private static int classCount = 3;          //TODO: Change to 0 default when database implemented
        public string Name;
        public string Location;
        public int Capacity;

        public override void EditDetails()
        {
            throw new NotImplementedException();
        }

        public bool Equals(MeetingSpace other)
        {
            if (Name == other.Name
                && Location == other.Location
                && Capacity == other.Capacity)
            { 
                return true; 
            }
            else 
            { 
                return false; 
            }
        }

        public override int GenerateNewId()
        {
            throw new NotImplementedException();
        }

        public override void GetAll()
        {
            throw new NotImplementedException();
        }
    }
}
BookingEntityBase.cs MeetingSpace继承此类的id字段

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BookingApp.Core
{
    public abstract class BookingEntityBase
    {
        public int Id { get; set; }

    }
}

推荐答案

我测试了您的代码,并能够通过修改您的MeetingSpace类修复该问题。它包含的是字段而不是属性,我认为这就是它搞砸数据绑定的原因。

因此更改此设置:

public class MeetingSpace : MeetingObjectsBaseClass, IEquatable<MeetingSpace>
{
    public string Name;
    public string Location;
    public int Capacity;
    ...

至此:

public class MeetingSpace : MeetingObjectsBaseClass, IEquatable<MeetingSpace>
{
    public string Name { get; set; }
    public string Location { get; set; }
    public int Capacity { get; set; }
    ...

并且数据绑定应该正常工作。

这篇关于无法在剃须刀页面中保存表单详细信息,如Pluralsight基础知识课程中所示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆