两个动作中的对象不同

发布于 2024-12-12 13:10:33 字数 3027 浏览 0 评论 0原文

我有一个添加视图,该视图上传文件,该文件是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

动次打次papapa 2024-12-19 13:10:33

QuestionRevisions 未正确绑定的原因是相应的 textarea 字段没有正确的名称,因此默认模型绑定器无法绑定它。您可以查看以下博客文章,解释什么是集合的正确有线格式。

因此,要解决此问题,一种可能的方法是将视图模型中的类型从 ICollection 更改为 IList,以便您可以通过索引器访问集合,然后在视图中简单地显示:

<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>

请注意,文本区域的名称如何遵循默认模型绑定器与集合一起使用所需的有线格式。现在,当您提交表单时,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> to IList<QuestionRevision> so that you have indexer access to elements of the collection and then in the view simply:

<div class="editor-field">
    @Html.TextAreaFor(x => x.QuestionRevisions[0].Content)
    @Html.ValidationMessageFor(x => x.QuestionRevisions[0].Content)
</div>

Now the generated HTML will look like this:

<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>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文