我一直在關注 Scott Allen 的 Pluralsight 的 ASP.NET Core Fundamentals 課程,并且一直在使用我為一個專案創建的自己的程式來完成它。到目前為止,我的代碼與視頻中的代碼非常相似,只是我使用的是我自己的類物件而不是示例中的餐廳類。
我遇到的問題是在課程第 4 個模塊的模型系結視頻中。當我運行程式并在瀏覽器中編輯 Meeting Space 物件之一時,它會保存一個空/空物件,而不是使用表單中的輸入。我對包含 Update 方法的 MeetingSpaceData 類的單元測驗按預期作業,這讓我相信我在負責這些編輯的 Edit razor 頁面中犯了一個錯誤。
編輯:在 Update 方法中接收和使用的 MeetingSpace 物件(在 Edit.cshtml.cs 中呼叫)是默認的 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);
}
}
}
編輯.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>
編輯.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();
}
}
}
會議空間.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; }
}
}
uj5u.com熱心網友回復:
我測驗了您的代碼,并能夠通過修改您的 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; }
...
并且資料系結應該正常作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/325412.html
