gpt4 book ai didi

asp.net-mvc-4 - 在 ASP.NET MVC 4 中填充和选择下拉列表值

转载 作者:行者123 更新时间:2023-12-01 22:17:21 25 4
gpt4 key购买 nike

ASP.NET MVC 中的下拉列表让我感到困惑。我有一个模特类。模型类有一个名为 SelectedValue 的字符串。该字符串代表先前选择的值。我有一个从数据库中提取的项目集合。集合中的每个项目都有一个 ID 和一个名称值。我的问题是,我不知道将此信息输入 UI 的推荐方法是什么。

我不确定是否应该使用 ViewBag 还是 Model。但一旦该值存在,我什至不确定填充 UI 的最佳方法是什么。我见过 HTML 助手和使用 RAZOR 语法的人。我很困惑。人们推荐什么?

谢谢

最佳答案

这就是我的做法,假设你有 2 个模型团队和玩家:

Player.cs

public class Player
{
[HiddenInput(DisplayValue = false)]
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

[Required]
[ForeignKey("Team")]
[Display(Name = "Team")]
public int TeamId { get; set; }

[Display(Name = "First name")]
public string FirstName { get; set; }

[Display(Name = "Last name")]
public string LastName { get; set; }

public virtual Team Team { get; set; }
}

Team.cs

 public class Team
{
[Key]
[HiddenInput(DisplayValue = false)]
public int TeamId { get; set; }

[Display(Name = "Full Name:")]
public string Name { get; set; }

public virtual ICollection<Player> Players { get; set; }
}

然后在你的 PlayerController 中:注意:团队需要存在才能创建玩家

    private void PopulateTeamsDropDownList(object selectedTeams = null)
{
var teamsQuery = from d in _dataSource.Teams
orderby d.Name
select d;
ViewBag.TeamID = new SelectList(teamsQuery, "TeamId", "Name", selectedTeams);
}

[HttpGet]
public ActionResult Create()
{
PopulateTeamsDropDownList();
return View();
}

[HttpPost]
public ActionResult Create(Player model)
{
if (ModelState.IsValid)
{
using (EFootballDb db = new EFootballDb())
{
try
{
var player = new Player
{
FirstName = model.FirstName,
LastName = model.LastName
};
db.Players.Add(player);
db.SaveChanges();
return RedirectToAction("About", "Home");
}
catch (Exception)
{
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
}
}
}
PopulateTeamsDropDownList(model.TeamId);
return View(model);
}

最后,您的“为播放器创建 View ”将如下所示:

@{
ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
<legend>Player</legend>

<div class="editor-label">
@Html.LabelFor(model => model.TeamId, "Pick Your Team")
</div>
<div class="editor-field">
@Html.DropDownList("TeamId", String.Empty)
@Html.ValidationMessageFor(model => model.TeamId)
</div>

<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>

<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>

<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

关于asp.net-mvc-4 - 在 ASP.NET MVC 4 中填充和选择下拉列表值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15441127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com