Click here to Skip to main content
15,919,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having a problem creating a blog.

The problem is with the AddToPost in the update part of the PostsController code, it is not recognizing the AddToPost Code. I am getting a missing directive or assembly reference, does anyone have any ideas. I did try just replacing the AddToPost with Add, I got the same error, any suggestions?

The code is as follows:

C#
public ActionResult Update(int? id, string title, string body, DateTime datetime, string tags)
        {
            if (!IsAdmin)
            {
                return RedirectToAction("Index");
            }

            Post post = GetPost(id);
            post.Title = title;
            post.Body = body;
            post.DateTime = datetime;
            post.Tags.Clear();

            tags = tags ?? string.Empty;
            string[] tagNames = tags.Split(new char[] { ' ' },                    StringSplitOptions.RemoveEmptyEntries);
            
            foreach (string tagName in tagNames)
            {
                post.Tags.Add(GetTag(tagName));
            }
            if (!id.HasValue)
            {
                model.AddToPosts(post);
            }

            model.SaveChanges();
            return RedirectToAction("Details", new { id = post.ID });
        }


Thanks, again look forward to hearing from you soon.
Posted
Comments
Member 9142936 19-Feb-14 17:33pm    
Thanks for your input, I solved the problem by changing the code to "model.Posts.Add(post);" from "model.AddToPost(post);" it seems that MVC3 will not accept "AddToPost" and it wants the routing identified properly by using "model.Posts".

It looks like you have created an ASP.NET MVC project with visual studio. If so, i know the convention that is used by default (you could have done something different) is to have the methods in the controller match the view name.

So for example. I have a project with a ReviewController.
So, in my project i have a Controllers folder and a ReviewController.cs file

Inside that ReviewController.cs are 2 edit methods:
1) public ActionResult Edit(int id = 0)
2) [HttpPost]
public ActionResult Edit(Review review, FormCollection values)

In my project i have a Views folder with a Review folder.
Inside the Review folder is an Edit.chstml file.

Without going into a lot of details, your controller name, controller methods, and view name have to "match" correctly. That is how MVC knows how to route things.

Anyways, that is what it sounds like to me. Sorry if i am on the wrong track...
 
Share this answer
 
Thanks for your input, I solved the problem by changing the code to "model.Posts.Add(post);" from "model.AddToPost(post);" it seems that MVC3 will not accept "AddToPost" and it wants the routing identified properly by using "model.Posts".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900