开发者

Should an MVC Controller and View have a 1 to 1 relationship?

I am currently using asp.NET MVC to build a Content Management System parts of a website and have the odious job of recreating all of my Views as Partial Views as it appears to be breaking the way the site map works.

For example, my FAQAdminController has the Views: Index, AddFAQ, EditFAQ, ConfirmDeleteFAQ and these have to become partial views.

I have been told that each Controller should have a single View and all other forms or actions that end in page content to be displayed should be implemented as Partial Views.

I have checked before posting and no documentation, articles, wikis or SO questions give me a straight answer as to whether or not having multipl开发者_运维百科e Views per Controller is breaking the MVC model.

Strictly, should an MVC Controller and View have a 1 to 1 relationship?


I have been told that each Controller should have a single View and all other forms or actions that end in page content to be displayed should be implemented as Partial Views.

That's nonsense.

Strictly, should an MVC Controller and View have a 1 to 1 relationship?

Absolutely not. Design your application in a logical and consistent way.

A controller usually handles many actions and returns many different views. You could consider having a FAQController to render all the views, for both user and admin.


I think that defeats the purpose of MVC. As controller should be able to decide which view to use to present model.


MVC Controller should be able to support more than one View.


MVC Controllers can and should support multiple views. It is helpful to have the views supported by a single controller relate to the same business object or process.

Consider the example below

public class ExampleController : Controller
{
    public ActionResult Index()
    {
            //get list of objects
            return View(); // uses a view called Index
    }

    public ActionResult Edit(string id)
    {
            Object target = ObjectManager.GetObject(id);
            if (target == null)
            {
                    return New();//view for creating a record
            }
            else
            {
                    ViewData.Add("detail", target);
                    return View("Detail");//using a view not name-mapped to controller method name
            }
    }

    public ActionResult New()
    {
            return View();  
    }

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜