Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to post an item to a collection that belongs to another model. This is the form in the view:

@using (Html.BeginForm("Create", "Comment", FormMethod.Post, new { enctype = "multipart/form-data", encoding = "multipart/form-data" }))
            {
                @Html.AntiForgeryToken()
                <div class="modal-body">
                    <div class="form-horizontal">
                        <input type="hidden" value="@Model.DeviceID" />
                        <div class="form-group">
                            <h5>Your name</h5>
                            <div class="col-md-10">
                                <input name="Author" />
                            </div>
                        </div>
                        <div class="form-group">
                            <h5>Comment</h5>
                            <div class="col-md-10">
                                <textarea name="Comment"></textarea>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">

                    <input type="submit" class="btn btn-primary" value="Submit" /> <!--Next</input>-->
                </div>
            }

and the controller code is set up as follows

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(string returnUrl, int devID, string Author, string text)
    {
        string url = this.Request.UrlReferrer.AbsolutePath;

        Comment comment = new Comment();

        if (ModelState.IsValid)
        {
            //db.Comments.Add(comment);
            db.SaveChanges();
            return Redirect(url);
        }
        return Redirect(url);
    }

However this doesnt work because I cant set the model id of the item that is getting posted.

What is the Asp.net MVC way to add to a virtual collection that a model owns?

share|improve this question
1  
Where do all these parameters for your Create action come from? Author is the only one that has a corresponding text box in your form. I see you are setting the DeviceId on a hidden input but that doesn't even have a name. –  Jason Nesbitt May 9 at 23:18
    
I had the same issue some time ago. Eventually I ended up putting the ID in a visible read-only input, which was located where the user can't see it. Can't remember if it was placed 'outside' the screen, or just behind some other element. That's an ugly solution, but does the job. It's also totaly unsafe, but that was admins page.anyway. –  shay__ 2 days ago
add comment

1 Answer

up vote 1 down vote accepted

What I have noticed the names don't match in your controller. So you don't retrieve it well. Change first this line:

 <input name="Author" /> and <textarea name="Comment"></textarea> 

By:

 <input name="author" /> and <textarea name="comments"></textarea>

Could you change the parameters to match the name from the view.

 public ActionResult Create(string returnUrl, int DeviceID, string author, string comments)
 {
    string url = this.Request.UrlReferrer.AbsolutePath;

    Comment myComment = new Comment();
    comment.DeviceID = DeviceID;
    comment.Author = author;
    comment.Comments= comments;// I presume your Comment class has thoses properties if not you can see at least the way you can deal with that 
    if (ModelState.IsValid)
    {
        db.Comments.Add(myComment);
        db.SaveChanges();
        return Redirect(url);
    }
    return Redirect(url);
}

I hope it will help. If you can show the code of your Comment model so I can edit that if it requires.

share|improve this answer
    
This looks to be right, though I thought there was a way to do it with the Asp.net helpers like Html.EditorFor() etc. The one think im not sure about is the DeviceID. I have A virtual collection for the Device: public virtual ICollection<Comment> Comments { get; set; } and the Device_ID is not exposed on an individual comment instance, so comment.Device_ID will not work. –  bischoffingston 11 hours ago
    
Nevermind I added it to my model and It popped up on intellisense, Thanks again for the response –  bischoffingston 11 hours ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.