两个动作中的对象不同
我有一个添加视图,该视图上传文件,该文件是 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
没有questionrevisions(为空)
为什么这个对象正在重置?
我在控制器中有一个字段:
PasmISOContext db = new PasmISOContext();
这是一个类 Question
public class Question { [钥匙] 公共 int Id { 得到;放;这
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);
}
}
I have a Add view which uploads file which is element of Question
class
@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" />
}
I have 2 actions in my controller
First action is when user clicks at Add button and he gets form to upload photo:
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);
}
and second action which fires when User accepts form:
[HttpPost]
public ActionResult Add(Question containers, HttpPostedFileBase file)
{
The problem is tha I see that Question
hasn't questionrevisions (is null)
Why this object is resetting?
I have a field in Controller:
PasmISOContext db = new PasmISOContext();
Here is a class Question
public class Question
{
[Key]
public int Id { get; set; }
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; }
}
Here is a definition of my context:
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
QuestionRevisions 未正确绑定的原因是相应的 textarea 字段没有正确的名称,因此默认模型绑定器无法绑定它。您可以查看以下博客文章,解释什么是集合的正确有线格式。
因此,要解决此问题,一种可能的方法是将视图模型中的类型从
ICollection
更改为IList
,以便您可以通过索引器访问集合,然后在视图中简单地显示:现在生成的 HTML 将如下所示:
请注意,文本区域的名称如何遵循默认模型绑定器与集合一起使用所需的有线格式。现在,当您提交表单时,
QuestionRevisions
列表将被正确绑定,并且它将包含单个元素。The reason the QuestionRevisions doesn't get properly bound is because the corresponding textarea field doesn't have a correct name so that the default model binder could bind it. You may take a look at the following blog post explaining what is the correct wire format for collections.
So to fix the problem one possibility is to change the type in your view model from
ICollection<QuestionRevision>
toIList<QuestionRevision>
so that you have indexer access to elements of the collection and then in the view simply:Now the generated HTML will look like this:
Notice how the name of the textarea respects the wire format expected by the default model binder to work with collections. Now when you submit the form the
QuestionRevisions
list will be correctly bound and it will contain a single element.