开发者

How do I link viewMasterPage with controller

I'm new to MVC, and I'm facing a problem how can I connect a controller "HelloWorld" to viewMasterPage "Index" .. and if I want all my pages to contain components I need to use viewMasterP开发者_JAVA技巧age for all views??

Edit I'm using MVC3 and Razor as view engine


Do it that way. I am assuming that you are using web forms view engine.

your master page is site.aspx file inside your Views/Shared folder. here is the your index file inside Views/HelloWorld folder.

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Bla bla bla..
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Index of HelloWord</h2>
    <%:ViewData["Message"]%>

</asp:Content>

here is what the HelloWorldController.cs file inside the controllers folder should look like;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace App.Controllers {

    public class HelloWorldController : Controller {

        public ActionResult Index() {

            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }

    }
}

EDIT : Here is the razor version of this;

your master page is _layout.cshtml file inside your Views/Shared folder. here is the your index.cshtml file inside Views/HelloWorld folder.

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
<p>
     @ViewBag.Message
</p>

here is what _ViewStart.cshtml file should looks like inside Views folder;

@{
    Layout = "~/Views/Shared/_Layout.cshtml";

}

here is what the HelloWorldController.cs file inside the controllers folder should look like;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace App.Controllers {

    public class HelloWorldController : Controller {

        public ActionResult Index() {

            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View();
        }

    }
}

NOTE : I am assuming here that your routes the default routes which visual studio creates for you out of the box when you create a new MVC internet application


The Master pages work like template files, most of parts are static and you only need to change a few places to show desire result. The changing parts, which are different for different view pages, are dynamically generated by the view engine. The action of generating the dynamic contents is called "Render".

When using Razor View Engine, you need to tell which parts are static and which parts are changing. For the changing part, you don't write anything but a magic call @RenderBody() For parts that are need in some view but not in others. You use @RenderSection("sectionName", false).

here is an example _layout.cshtml file.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   ... your style sheet files ... 
</head>

<body>
     ... shared components...     
    @RenderBody()
     ...I like to put the java script files to the end...     
    @RenderSection("extraScripts", required: false)
</body>
</html>

Then in each view that shares the same components you add

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

in the beginning of the file. The rest of your view file will become fetched by the @RenderBody() call. If you want to add some customized script for your view you add another section by

@section extraScripts{
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">      </script>
}

The extra script you add for this particulate page will be rendered at run time to your result page.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜