21

I have two meta tag in _Layout.cshtml master page and now i want to add meta tags in someone.cshtml view page.

and i also try with this code

put in _layout.cshtml master page @RenderSection("metatags",false);

put in someone.cshtml like @section metatags { <meta ... /> }

but not get success.

and also try with add meta tag jquery but not worked fine.

Bjarki Heiðar
  • 3,117
  • 6
  • 27
  • 40
Jatin Gadhiya
  • 706
  • 2
  • 7
  • 24

4 Answers4

58

It should work.

Here's my master page _Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    @RenderSection("metatags", false)
    <title>My ASP.NET Application</title>
</head>
<body>
    @RenderBody()
</body>
</html>

Here's the index.cshtml file

@section metatags
{
    <meta name="test" content="test"/>
    <meta name="test2" content="test"/>
}

<div>Page content</div>

The result

<html>
<head>
    <meta charset="utf-8">

    <meta name="test" content="test">
    <meta name="test2" content="test">

    <title>My ASP.NET Application</title>
</head>
<body>        

<div>Page content</div>    

</body>
</html>
meziantou
  • 20,589
  • 7
  • 64
  • 83
  • yes i have use this code but not working. see i used in _layout.cshtml { @RenderSection("metatags",false); } and use in someone.cshtml like @section metatags { } – Jatin Gadhiya Apr 21 '14 at 12:06
  • Try to replace `@RenderSection("metatags",false)` by `@RenderSection("metatags", true)` to ensure the section is used. – meziantou Apr 21 '14 at 12:15
  • 2
    Error : Section not defined: "metatags". in _Layout.cshtml – Jatin Gadhiya Apr 21 '14 at 12:18
  • Do you have more than one master page? – meziantou Apr 21 '14 at 12:20
  • yes.. _Root.cshtml and in this master page included _Root.Head.cshtml and and i am add this { @RenderSection("metatags",required : true);} in _Root.Head.cshtml because in this page already added head tag and also scripts and css – Jatin Gadhiya Apr 21 '14 at 12:24
  • `RenderSection` can only be used in Layout file (e.g. MasterPage), not partial files. http://stackoverflow.com/questions/7808377/the-file-views-position-edit-cshtml-cannot-be-requested-directly-because-it – meziantou Apr 21 '14 at 12:56
  • so how to do now in this situation because i have a multiple master page.. give me any solution. please – Jatin Gadhiya Apr 21 '14 at 13:06
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/51106/discussion-between-jatin-gadhiya-and-meziantou) – Jatin Gadhiya Apr 21 '14 at 13:27
16

I found a way that works for now.

This solution mostly use when multiple master page.

In the controllers / action assign whatever meta info you need for that view.

ViewBag.Title = "some title";
ViewBag.MetaDescription = "some description";

In the View or master page get whatever meta info you need for that view.

 @if(ViewBag.MetaDescription != null)
    {
        <meta name="description" content="@ViewBag.MetaDescription" />
    }

    @if(ViewBag.MetaKeywords != null)
    {
        <meta name="keywords" content="@ViewBag.MetaKeywords" />
    }
Jatin Gadhiya
  • 706
  • 2
  • 7
  • 24
1

If are using nested layouts you'll need to follow this guideline:

@RenderSection in nested razor templates

The technique you're using should work, if it is not, maybe that's the reason.

But looking at the intended use in your own answer, if you only need to modify the keywords and description tags there are apis in NopCommrece for that.

In your master layout:

<meta name="description" content="@(Html.NopMetaDescription())" />
<meta name="keywords" content="@(Html.NopMetaKeywords())" />

and in the client cshtml files

@{
    Html.AddMetaDescriptionParts(Model.MetaDescription);
    Html.AddMetaKeywordParts(Model.MetaKeywords);
}

There are plenty of samples for this in the NopCommerce code.

Community
  • 1
  • 1
Marco Regueira
  • 1,045
  • 8
  • 17
0

here is a good sample on how to dynamically creating meta tags in asp.net mvc:

public string HomeMetaTags()
{
    var strMetaTag = new StringBuilder();
    strMetaTag.AppendFormat(@"<meta content='{0}' name='Keywords'/>","Home Action Keyword");
    strMetaTag.AppendFormat(@"<meta content='{0}' name='Descption'/>", "Home Description Keyword");
    return strMetaTag.ToString();
}

public ActionResult Index()
{
    ViewBag.Message = "Welcome to ASP.NET MVC!";
    ViewBag.MetaTag = HomeMetaTags(); 
    return View();
}


<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
   @Html.Raw(ViewBag.MetaTag)
</head>
Mazdak Shojaie
  • 1,620
  • 1
  • 25
  • 32