gpt4 book ai didi

c# - 表单发送空模型

转载 作者:行者123 更新时间:2023-11-30 17:47:46 25 4
gpt4 key购买 nike

我有一个要在我的 View 上修改的对象列表,我构建了一个 View 模型来包含对象列表:

public class TrainerListViewModel
{
public List<Trainer> Trainers { get; set; }
}

然后我从 Controller 向 View 发送培训师列表:

 public virtual ActionResult Social()
{
var Trainers = new TrainerListViewModel();

Trainers.Trainers = (from t in _db.Trainers select t).ToList();

return View(Trainers);
}

这是我的观点:

@model XStreamingFitness.ViewModels.TrainerListViewModel


@using (Html.BeginForm("Social", "Participant", FormMethod.Post))
{
for (int i = 0; i < Model.Trainers.Count; i++)
{
<div class="formSep">

<div class="editor-label">
@Html.LabelFor(m => m.Trainers[i].permissionPostFacebook)
</div>
<div class="editor-field">
@Html.EditorFor(m => m.Trainers[i].permissionPostFacebook)
@Html.ValidationMessageFor(m => m.Trainers[i].permissionPostFacebook)
</div>
</div>
}

<input type="submit" value="Save Settings" name="Submit" />
}

现在这里是 POST Controller 方法:

[HttpPost]
public virtual ActionResult Social(TrainerListViewModel Trainers)
{

return RedirectToAction("Profile");
}

但每次我提交时,Trainers 模型都是空的,我不确定为什么会发生这种情况。

最佳答案

这个问题之前已经被问过所以我建议你检查mvc3 submit model empty ,其原理与您遇到的问题相同。

查看帖子中的片段。

I see people writing the following lambda expression modelItem => item.SomeProperty in their views very often and asking why the model binder doesn't correctly bind collection properties on their view models.

This won't generate proper name for the checkbox so that the default model binder is able to recreate the Settings collection. I would recommend you reading the following blog post to better understand the correct format that the model binder expects.

-达林季米特洛夫

这与您构建表单的方式有关,他建议您为 Trainer 对象使用属性编辑器模板。

这应该可以工作。

希望对你有帮助

关于c# - 表单发送空模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24311072/

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