2012-10-11 20 views
5

Có thể nối mã HTML vào phần dao cạo hiện có không?Nối mã HTML vào phần Dao cạo hiện có

Dưới đây là kịch bản của tôi:

_layout.cshtml My chứa một cái gì đó như thế này:

@RenderSection("BottomSection", required: false) 

và trong một trong những quan điểm - _article.cshtml, tôi đã xác định phần như dưới đây:

@section BottomSection 
{ 
<script src='~/Scripts/module/article_details.js' type='text/javascript'></script> 
<script src='~/Scripts/module/modal.js' type='text/javascript'></script> 
@MvcHtmlString.Create(Model.ExtraStuff) 
} 

và trong chế độ xem một phần có tên _counter.cshtml, được sử dụng bởi chế độ xem ở trên; Tôi muốn thêm mã HTML khác vào cùng một phần, tức là, BottomSection.

tôi đã cố gắng tuyên bố phần BottomSection một lần nữa trong giao diện phần:

@section BottomSection{ 
<text>More data</text> 
} 

Nhưng nó không làm việc ra.

Có cách nào để đạt được điều này - tự động nối thêm mã vào phần dao cạo đã được xác định trong MVC 4 không? Xin lưu ý rằng chế độ xem một phần không mong đợi bất kỳ dữ liệu nào từ chế độ xem/mô hình gốc. Và tôi đang sử dụng MVC 4 với .Net Framework 4.0/VS2010.

Trả lời

-3

Sử dụng Ajax bạn có thể tải chế độ xem một phần và có thể hiển thị trong bộ phận mục tiêu của bạn.

Hãy thử sử dụng jquery ajax

 $.ajax({ 
      type: 'GET', 
      url: '@Url.Action("Action","Controller")', 
      cache: false, 
      timeout: 20000, 
      contentType: "application/json; charset=utf-8", 
      success: function (_results) { 
       $("#TargetDiv").html(_results); 
      }, 
      error: function (_results) { 

      } 
     }); 
+0

Please Tell tại sao giải pháp này là sai. Tôi đã thử tương tự cho việc thêm html mới được tạo ra vào giao diện đã hiển thị – amesh

+0

Cảm ơn bạn đã trả lời, nhưng đây không phải là tôi muốn làm. Chế độ xem một phần có thể thêm nội dung tham khảo thêm nội dung/tập lệnh, v.v. Bắt dữ liệu sẽ thêm yêu cầu khứ hồi và cho đến lúc đó trang của tôi sẽ không hoạt động! – Kay

-2

Có lẽ tôi không hiểu câu hỏi của bạn, nhưng tại sao bạn không sử dụng xem lồng nhau một phần ???

ví dụ:

PartialView1

`<script src='~/Scripts/module/article_details.js' type='text/javascript'></script> 
<script src='~/Scripts/module/modal.js' type='text/javascript'></script> 
@MvcHtmlString.Create(Model.ExtraStuff) 
@{Html.RenderPartial("PartialView2",Model.ExtraStuff);}` 

PartialView2

`<text>More data</text>` 
+0

Có nhiều chế độ xem một phần sẽ giải quyết vấn đề. Tuy nhiên, điều này sẽ kết thúc có rất nhiều các tập tin xem một phần cho tôi - bất chấp mục đích cho cách tiếp cận năng động. – Kay

0

Tôi không biết làm thế nào để thêm công cụ để phần (trong thực tế, tôi muốn biết rằng bản thân mình), nhưng tôi biết một mẹo có thể tạo ra kết quả tương tự. Thay vì sử dụng các phần, người ta có thể sử dụng TempData. TempData rất giống với ViewBag, nhưng một khi biến được đặt, nó sẽ tồn tại ở đó cho người dùng hiện tại cho đến khi một người cố gắng truy cập lại nó (nó có thể tồn tại qua một vài yêu cầu liên tiếp cho người dùng hiện tại, vì vậy bạn nên thận trọng hơn). Dưới đây là một ví dụ về cách nó có thể được sử dụng.

Trong bố cục:

@Html.Raw(new MvcHtmlString((string)TempData["BottomSection"])); 

Trong giao diện:

@{ 
    var bottomSection = (string)TempData["BottomSection"]; 
    if (bottomSection == null) 
    { 
     bottomSection = ""; 
    } 
    bottomSection += "<script src='~/Scripts/module/article_details.js' type='text/javascript'></script>\n"; 
    bottomSection += "<script src='~/Scripts/module/modal.js' type='text/javascript'></script>\n"; 
    bottomSection += Model.ExtraStuff + "\n"; 
    TempData["BottomSection"] = bottomSection; 
} 

Trong giao diện phần:

@{ 
    var bottomSection = (string)TempData["BottomSection"]; 
    if (bottomSection == null) 
    { 
     bottomSection = ""; 
    } 
    bottomSection += "More data"; 
    TempData["BottomSection"] = bottomSection; 
} 

Điều này có thể được cải thiện hơn nữa bằng cách viết một helper cho những bộ phận giả và \ hoặc bằng cách di chuyển nội dung của các phần một phần riêng biệt (xem bên dưới).

bottomSection += Html.Partial("_StuffToAddToSection").ToString(); 

lớp Helper:

public static class PseudoSectionsHelper 
{ 
    public static MvcHtmlString AppendToPseudoSection<T>(this TempDataDictionary TempData, string sectionName, T model, Func<T, HelperResult> content, bool addNewLineCharacter = true) 
     where T : class 
    { 
     return AppendToPseudoSection(TempData, sectionName, content(model).ToString(), addNewLineCharacter); 
    } 

    public static MvcHtmlString AppendToPseudoSection(this TempDataDictionary TempData, string sectionName, MvcHtmlString content, bool addNewLineCharacter = true) 
    { 
     return AppendToPseudoSection(TempData, sectionName, content.ToString(), addNewLineCharacter); 
    } 

    public static MvcHtmlString AppendToPseudoSection(this TempDataDictionary TempData, string sectionName, string content, bool addNewLineCharacter = true) 
    { 
     var section = (string)TempData[sectionName]; 
     if (section == null) 
     { 
      section = ""; 
     } 
     else if (addNewLineCharacter) 
     { 
      section += "\n"; 
     } 
     section += content; 
     TempData[sectionName] = section; 
     // We return empty MvcHtmlString to be able to use this helper inline (without declaring code block @{ some code... } in view) 
     return new MvcHtmlString(""); 
    } 

    public static MvcHtmlString PseudoSection(this TempDataDictionary TempData, string sectionName) 
    { 
     var section = (string)TempData[sectionName]; 
     return new MvcHtmlString(section); 
    } 
} 

Sử dụng ví dụ

Trong bố trí thêm:

@TempData.PseudoSection("BottomSection") 

Theo quan điểm:

@TempData.AppendToPseudoSection("BottomSection", Model, @<text> 
    <script src='~/Scripts/module/article_details.js' type='text/javascript'></script> 
    <script src='~/Scripts/module/modal.js' type='text/javascript'></script> 
    @MvcHtmlString.Create(Model.ExtraStuff) 
</text>) 

hoặc

@{ 
    TempData.AppendToPseudoSection("BottomSection", Model, @<text> 
     <script src='~/Scripts/module/article_details.js' type='text/javascript'></script> 
     <script src='~/Scripts/module/modal.js' type='text/javascript'></script> 
     @MvcHtmlString.Create(Model.ExtraStuff) 
    </text>); 
} 

hoặc thậm chí

@TempData.AppendToPseudoSection("BottomSection", Html.Partial("BottomSectionScriptsAndStuff")) 

Và trong phần:

@TempData.AppendToPseudoSection("BottomSection", "More data") 
+0

Sử dụng System.Web.HttpContext.Current.Items thay cho TempData có thể là một ý tưởng hay hơn, vì TempData tồn tại miễn là bạn không đọc nó có thể dẫn đến các tình huống kỳ lạ (như một phần bạn không làm trên một trang, hiển thị trên trang khác). – jahu