开发者

Do I need a shared view for navigation between controllers?

开发者 https://www.devze.com 2023-02-17 08:37 出处:网络
I\'m very new to ASP.NET MVC and this is probably a really obvious thing...Basically, I can开发者_StackOverflow社区\'t access the \"Forum\" view which I had created inside \"Home\" folder (because I n

I'm very new to ASP.NET MVC and this is probably a really obvious thing...Basically, I can开发者_StackOverflow社区't access the "Forum" view which I had created inside "Home" folder (because I need a link to Forum on my main homepage.). I was wondering if it's okay to just move the Forum view into the Shared folder? Is this somehow very bad practice? It seems like I have strong coupling now, because on the one hand the forum view gets called inside HomeController, and on the other hand it will pretty much always be used with ForumController from now on. So that might be unnecessary/wrong somehow?

edit: If I do this, the URLs change in a weird way, there must be a better solution right? First when I click on forum link on main page, I'm at: /Home/Forum. I look at the forum, everything is fine. Now I click on "Post a topic", and after the roundtrip I'm at /Forum/PostTopic. Home is gone.


If you have a ForumController its associated views need to be located at ~/Views/Forum.

For example:

public class ForumController: Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

and then you would have the corresponding view in ~/Views/Forum/Index.cshtml:

@{
    ViewBag.Title = "Forum";
}

<h2>Forum</h2>
<p>
     Put content here.
</p>

and finally you generate a link to the Index action of the ForumController like this:

@Html.ActionLink("Go to forum", "Index", "Forum")
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号