Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6763815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:31:17+00:00 2026-05-26T14:31:17+00:00

I have a Add view which uploads file which is element of Question class

  • 0

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);
        }

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:31:18+00:00Added an answer on May 26, 2026 at 2:31 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Excel add-in which I add so many class modules that it is
I have a Django 'add business' view which adds a new business with an
I have a link_tag in view which add rendered content to page link_to("add",nil,:id=>"create_row_cycle",:onclick=>"$('div#cycle_form table').append('#{escape_javascript(render(:partial=>'cycles',
I have a view which I would like to add an exposed filter of
I have an element which displays a comment form, the action is comments/add. If
I have a UINavigationController in which I have to add a view controller to
I have a for loop that creates multiple UIImageViews that I add to self.view
I have a view that I want to add some custom drawing to. I
I have a form in witch users can add their working hours view them
Here's my case: I have a table view showing contacts. Add button in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.