我有一个上传文件的添加视图,该文件是Question
类
@model PasmISO.Domain.Question
@using (Html.BeginForm("Add", "Photo", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="editor-field">
@Html.TextAreaFor(model => model.QuestionRevisions.First().Content)
@Html.ValidationMessageFor(model => model.QuestionRevisions.First().Content)
</div>
<input type="file" name="file" id="file" />
<br/>
<input type="submit" value="Ask" />
}
我的控制器中有2个动作
第一个操作是当用户点击“添加”按钮并获取上传照片的表单时
public ActionResult Add()
{
db.Users.Add(new User() { Avatar = new Avatar() { Link = new Uri("http://myUrl/%2E%2E/%2E%2E") }, CreationDate = DateTime.Now, LastActivityDate = DateTime.Now, LastLoginDate = DateTime.Now });
db.SaveChanges();
db.Users.Local.First().Questions = new Collection<Question>() { new Question() };
var question = new Question();
question.QuestionRevisions = new Collection<QuestionRevision>();
var questionRevision = new QuestionRevision();
questionRevision.Tags = new Collection<Tag>();
question.QuestionRevisions.Add(questionRevision);
return View(question);
}
以及用户接受表单时触发的第二个操作:
[HttpPost]
public ActionResult Add(Question containers, HttpPostedFileBase file)
{
问题是,我发现Question
没有问题规则(为空)
为什么要重置此对象?
我在Controller中有一个字段:
PasmISOContext db = new PasmISOContext();
这是一个班级Question
公共课问题 { [键] public int Id {get;组; }
public virtual User Creator { get; set; }
public int CreatorId { get; set; }
public int PhotoId { get; set; }
public virtual ICollection<QuestionRevision> QuestionRevisions { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
public virtual ICollection<QuestionVote> Votes { get; set; }
public virtual Photo Photo { get; set; }
}
以下是我的背景的定义:
public class PasmISOContext : DbContext
{
public DbSet<Avatar> Avatars { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<Question> Questions { get; set; }
public DbSet<QuestionRevision> QuestionRevisions { get; set; }
public DbSet<Photo> Photos { get; set; }
public DbSet<Achievement> Achievements { get; set; }
public DbSet<Comment> Comments { get; set; }
public DbSet<Tag> Tags { get; set; }
public DbSet<QuestionVote> QuestionVotes { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
答案 0 :(得分:1)
QuestionRevisions未正确绑定的原因是因为相应的textarea字段没有正确的名称,因此默认模型绑定器可以绑定它。您可以查看following blog帖子,了解正确的集合电汇格式。
因此,要解决此问题,可以将视图模型中的类型从ICollection<QuestionRevision>
更改为IList<QuestionRevision>
,以便您可以使用索引器访问集合的元素,然后在视图中:< / p>
<div class="editor-field">
@Html.TextAreaFor(x => x.QuestionRevisions[0].Content)
@Html.ValidationMessageFor(x => x.QuestionRevisions[0].Content)
</div>
现在生成的HTML将如下所示:
<div class="editor-field">
<textarea cols="20" name="QuestionRevisions[0].Content" id="QuestionRevisions_0__Content" rows="2">Some value</textarea>
<span class="field-validation-valid" data-valmsg-for="QuestionRevisions[0].Content" data-valmsg-replace="true"></span>
</div>
请注意textarea的名称如何遵循默认模型绑定程序用于处理集合所期望的有线格式。现在,当您提交表单时,QuestionRevisions
列表将被正确绑定,并且它将包含单个元素。