gpt4 book ai didi

asp.net-mvc - 带有模型的 MVC 3 表单回发

转载 作者:行者123 更新时间:2023-12-04 17:23:59 25 4
gpt4 key购买 nike

我正在尝试将我的模型发布回 Controller ,但 FormCollection 中的结果不是我所期望的。

我收到了从我的自定义 XML 服务返回的 TeamModel 的 IEnumerable。每个 TeamModel 都包含一个字符串和一个 bool 值。字符串是团队的名称, bool 表示用户是否要将其插入数据库。

Controller :

public ActionResult ImportTeams()
{
var xmlService = new XmlSoccerService();
const string league = "Scottish Premier League";

var model = xmlService.GetAllTeamsByLeagueAndSeason(league, "1112");
ViewBag.League = league;
return View("~/Views/Admin/Database/ImportTeams.cshtml", model);
}

[HttpPost]
public ActionResult ImportTeams(FormCollection collection , string league)
{
return RedirectToAction("ImportTeams");
}

团队模型:

public class TeamModel
{
public string Name { get; set; }
public bool Import { get; set; }
}

查看:

@model IEnumerable<TopTipFootball.XmlSoccer.Models.TeamModel>

@{
ViewBag.Title = "Import Teams";
}

<h2>Select the teams to import into: @ViewBag.League</h2>

@using (Html.BeginForm())
{
var league = ViewBag.League;

@Html.HiddenFor(l => league)

foreach (var team in Model)
{
<div class="editor-field">
@Html.EditorFor(model => team.Import)
@Html.DisplayFor(m => team.Name)
</div>
}
<p>
<input type="submit" value="Import teams" />
</p>
}

View 呈现的 html 中的一个元素:

<input checked="checked" class="check-box" data-val="true" data-val-required="The Import field is required." id="team_Import" name="team.Import" type="checkbox" value="true" />
<input name="team.Import" type="hidden" value="false" />
Aberdeen

一些问题:

  1. 如何确保在回发给 Controller 的帖子中取回 TeamModel 的 IEnumerable?
  2. 我是否打算以正确的方式将联盟传回 Controller ? (通过隐藏字段)

最佳答案

尝试使用 for 循环代替循环,即

for(int i = 0; i < Model.Count; i++) 
{
@Html.EditorFor(model => Model[i].Import)
@Html.DisplayFor(m => Model[i].Name)
}

我相信这应该创建 id 和名称,例如 Model_0_Import 等,希望这将使它在您的帖子上正确绑定(bind)。

是的,我以这种方式使用了隐藏字段。我当然假设联赛发布正确,只是项目列表不正确?

编辑:您总是可以继续使用 viewModel 而不是使用 viewBag 和 Model 的组合?

这是一个可能会提供一些帮助的解决方案吗?

public ActionResult ImportTeams()
{
const string league = "Scottish Premier League";

var viewModel = new LeagueTeamViewModel
{
League = league;
}

var xmlService = new XmlSoccerService();
var model = xmlService.GetAllTeamsByLeagueAndSeason(league, "1112");

viewModel.Teams.AddRange(xmlService.GetAllTeamsByLeagueAndSeason(league, "1112").Select(p => new TeamViewModel
{
Name = p.Name,
Import = p.Import
};

return View("ImportTeams", viewModel);
}

[HttpPost]
public ActionResult ImportTeams(LeagueTeamViewModel viewModel)
{
}

public class LeagueTeamViewModel
{
public string League { get; set; }

private List<TeamViewModel> _teams = new List<TeamViewModel>();

public List<TeamViewModel> Teams
{
get { return _teams; }
set { _teams = value; }
}
}

public class TeamViewModel
{
[DisplayName("Name")]
public string Name { get; set; |

[DisplayName("Imported")]
public string Import { get; set; |
}

还有你的看法

@model IEnumerable<LeagueTeamViewModel>
@{
ViewBag.Title = "Import Teams";
}

<h2>Select the teams to import into: @Model.League</h2>

@using (Html.BeginForm())
{
@Html.HiddenFor(model => model.League)

for(int i = 0; i < Model.Teams.Count; i++)
{
<div class="editor-field">
@Html.EditorFor(model => model.Teams[i].Import)
@Html.HiddenFor(m => model.Teams[i].Name)
</div>
}
<p>
<input type="submit" value="Import teams" />
</p>
}

关于asp.net-mvc - 带有模型的 MVC 3 表单回发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13464581/

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