如何更正AddToPost? [英] How to correct the AddToPost?

查看:84
本文介绍了如何更正AddToPost?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建博客时遇到问题。



问题在于PostsController代码的更新部分中的AddToPost,它无法识别AddToPost代码。我得到一个缺失的指令或汇编参考,有没有人有任何想法。我确实尝试用Add替换AddToPost,我得到了同样的错误,有什么建议吗?



代码如下:



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:

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.

推荐答案

看起来你已经用visual studio创建了一个ASP.NET MVC项目。如果是这样,我知道默认使用的约定(你可能做了不同的事情)就是让控制器中的方法与视图名称匹配。



所以例如。我有一个带有ReviewController的项目。

所以,在我的项目中,我有一个Controllers文件夹和一个ReviewController.cs文件



在ReviewController.cs里面有两种编辑方法:

1)public ActionResult Edit(int id = 0)

2)[HttpPost]

public ActionResult Edit(Review review,FormCollection values)



在我的项目中,我有一个带有Review文件夹的Views文件夹。

Review文件夹里面是一个Edit.chstml文件。



没有详细介绍你的控制器名称,控制器方法,和视图名称必须正确匹配。这就是MVC如何知道如何路由东西。



无论如何,这听起来像是对我来说。对不起,如果我在错误的轨道...
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...


感谢您的输入,我通过将代码更改为model.Posts.Add(post);解决了问题。来自model.AddToPost(post);似乎MVC3不会接受AddToPost,它希望通过使用model.Posts正确识别路由。
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".


这篇关于如何更正AddToPost?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆