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

标签 asp.net-mvc asp.net-mvc-3

我正在尝试将我的模型发布回 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/

相关文章:

c# - LINQ 查询 C# ASP

jquery - 如何在 ASP.NET MVC 3 和 JQuery 中基于单选按钮选择验证文本框?

c# - 使用 javascript 删除 webgrid 行?

asp.net-mvc-3 - 执行子请求时出错

c# - 在 SQL Server 中生成看似随机的唯一数字 ID

c# - 下拉列表 - MVC3

javascript - 如何使用 Asp.net MVC 上传和注入(inject)图像到 tinymce 4

c# - MVC 5 中的 ASP.NET 成员(member)提供程序

c# - 带有 Entity Framework 的 ASP.NET MVC - 更新/保存复杂类型属性

jquery - 自定义验证未按预期触发