如何将页面模型对象 属性 连接到 <select> 标签中的对象列表(来自数据库)? ASP.NET 剃刀页面
How to connect page model object property to list of objects (from db) in <select> tag? ASP.NET Razor Pages
武器型号:
public class Weapon {
public int Id { get; set; }
public string Name { get; set; }
public int BaseDamage { get; set; } // base damage with average attack
public int MagicDamage { get; set; }
public int ActionCost { get; set; }
public int HitChance { get; set; }
}
生物模型包含一些简单的属性,例如 Id 和武器。
我正在尝试使用数据库中的武器创建下拉列表,并在通过按钮提交表单时将 selected 武器添加到 Creature 对象。
页面模型包含生物对象和武器列表:
public Creature Creature { get; set; }
public IEnumerable<Weapon> Weapons { get; set; }
OnGet 方法加载 selected 生物和武器列表。
public void OnGetDetails(int id, int toggle) {
Creature = new GetCreature(_ctx).Get(id);
LastToggle = toggle;
Weapons = new GetWeapons(_ctx).Get();
}
第一个想法:
<form method="post">
<input type="hidden" asp-for="Creature.Id"/>
<select id="weapon" class="form-control bg-secondary text-light" asp-for="Creature.Weapon" asp-items="@Model.Weapons">
</select>
<button type="submit" class="btn btn-outline-warning m-2" asp-page-handler="UpdateWeapon">Wybierz</button>
</form>
但是 asp-items="@Model.Weapons" 不被 VS 接受:
CS0266:无法将类型 'System.Collection.Generic.IEnumerable<LastTemple.Models.Weapon>' 隐式转换为 'System.Collections.GenericIEnumerable<Microsoft.AspNetCore.Mvc.Rendering.SelectListItems>'。存在明确的对话(您是否缺少演员表?)
第二个想法(已更新 [以前的版本有 value="@item.Id"]):
<form method="post">
<input type="hidden" asp-for="Creature.Id"/>
<select id="weapon" class="form-control bg-secondary text-light" asp-for="Creature.Weapon">
@foreach (var item in Model.Weapons) {
<option value="@item">@item.Name - @item.BaseDamage | @item.MagicDamage | @item.ActionCost PA | @item.HitChance%</option>
}
</select>
<button type="submit" class="btn btn-outline-warning m-2" asp-page-handler="UpdateWeapon">Wybierz</button>
</form>
还是不行。不要抛出错误,但 Creature.Weapon 仍然为空,不是从 select 列表传递的。
发布方法:
public async Task<IActionResult> OnPostUpdateWeaponAsync() {
var item = _ctx.Creatures.Find(Creature.Id);
if (item == null)
{
return RedirectToPage("Creature");
}
if (Creature.Weapon == null)
{
return RedirectToPage("Creature");
}
await new EditCreature(_ctx).UpdateWeapon(Creature);
return RedirectToPage("Creature");
}
生物模型:https://github.com/Mlorism/LastTemple/blob/master/LastTemple/Models/Creature.cs
武器型号:https://github.com/Mlorism/LastTemple/blob/master/LastTemple/Models/Weapon.cs
页数:
https://github.com/Mlorism/LastTemple/blob/master/LastTemple/Pages/Manage/Creature.cshtml.cs
和
https://github.com/Mlorism/LastTemple/blob/master/LastTemple/Pages/Manage/Creature.cshtml
Second idea:
<form method="post">
<input type="hidden" asp-for="Creature.Id" />
<select id="weapon" class="form-control bg-secondary text-light" asp-for="Creature.Weapon">
@foreach (var item in Model.Weapons)
{
<option value="@item.Id">@item.Name - @item.BaseDamage | @item.MagicDamage | @item.ActionCost PA | @item.HitChance%</option>
}
</select>
<button type="submit" class="btn btn-outline-warning m-2" asp-page-handler="UpdateWeapon">Wybierz</button>
</form>
Still don't work. Don't throw errors but Creature.Weapon remains null,
is not passed from select list.
您绑定的是武器ID,所以
改变
asp-for="Creature.Weapon"
到
asp-for="Creature.Weapon.Id"
武器型号:
public class Weapon {
public int Id { get; set; }
public string Name { get; set; }
public int BaseDamage { get; set; } // base damage with average attack
public int MagicDamage { get; set; }
public int ActionCost { get; set; }
public int HitChance { get; set; }
}
生物模型包含一些简单的属性,例如 Id 和武器。
我正在尝试使用数据库中的武器创建下拉列表,并在通过按钮提交表单时将 selected 武器添加到 Creature 对象。
页面模型包含生物对象和武器列表:
public Creature Creature { get; set; }
public IEnumerable<Weapon> Weapons { get; set; }
OnGet 方法加载 selected 生物和武器列表。
public void OnGetDetails(int id, int toggle) {
Creature = new GetCreature(_ctx).Get(id);
LastToggle = toggle;
Weapons = new GetWeapons(_ctx).Get();
}
第一个想法:
<form method="post">
<input type="hidden" asp-for="Creature.Id"/>
<select id="weapon" class="form-control bg-secondary text-light" asp-for="Creature.Weapon" asp-items="@Model.Weapons">
</select>
<button type="submit" class="btn btn-outline-warning m-2" asp-page-handler="UpdateWeapon">Wybierz</button>
</form>
但是 asp-items="@Model.Weapons" 不被 VS 接受:
CS0266:无法将类型 'System.Collection.Generic.IEnumerable<LastTemple.Models.Weapon>' 隐式转换为 'System.Collections.GenericIEnumerable<Microsoft.AspNetCore.Mvc.Rendering.SelectListItems>'。存在明确的对话(您是否缺少演员表?)
第二个想法(已更新 [以前的版本有 value="@item.Id"]):
<form method="post">
<input type="hidden" asp-for="Creature.Id"/>
<select id="weapon" class="form-control bg-secondary text-light" asp-for="Creature.Weapon">
@foreach (var item in Model.Weapons) {
<option value="@item">@item.Name - @item.BaseDamage | @item.MagicDamage | @item.ActionCost PA | @item.HitChance%</option>
}
</select>
<button type="submit" class="btn btn-outline-warning m-2" asp-page-handler="UpdateWeapon">Wybierz</button>
</form>
还是不行。不要抛出错误,但 Creature.Weapon 仍然为空,不是从 select 列表传递的。
发布方法:
public async Task<IActionResult> OnPostUpdateWeaponAsync() {
var item = _ctx.Creatures.Find(Creature.Id);
if (item == null)
{
return RedirectToPage("Creature");
}
if (Creature.Weapon == null)
{
return RedirectToPage("Creature");
}
await new EditCreature(_ctx).UpdateWeapon(Creature);
return RedirectToPage("Creature");
}
生物模型:https://github.com/Mlorism/LastTemple/blob/master/LastTemple/Models/Creature.cs
武器型号:https://github.com/Mlorism/LastTemple/blob/master/LastTemple/Models/Weapon.cs
页数:
https://github.com/Mlorism/LastTemple/blob/master/LastTemple/Pages/Manage/Creature.cshtml.cs
和
https://github.com/Mlorism/LastTemple/blob/master/LastTemple/Pages/Manage/Creature.cshtml
Second idea:
<form method="post"> <input type="hidden" asp-for="Creature.Id" /> <select id="weapon" class="form-control bg-secondary text-light" asp-for="Creature.Weapon"> @foreach (var item in Model.Weapons) { <option value="@item.Id">@item.Name - @item.BaseDamage | @item.MagicDamage | @item.ActionCost PA | @item.HitChance%</option> } </select> <button type="submit" class="btn btn-outline-warning m-2" asp-page-handler="UpdateWeapon">Wybierz</button> </form>
Still don't work. Don't throw errors but Creature.Weapon remains null, is not passed from select list.
您绑定的是武器ID,所以
改变
asp-for="Creature.Weapon"
到
asp-for="Creature.Weapon.Id"