2013-03-14 76 views
0

我有以下操作來調用單個頁面中的多個部分視圖。如何使用MVC操作將HtmlPrefix添加到視圖?

public ActionResult AddDetail(string Key) 
{ 
    var viewData = new ViewDataDictionary(ViewData) 
      { 
       TemplateInfo = new System.Web.Mvc.TemplateInfo 
       { 
        HtmlFieldPrefix = string.Format("{0}", key) 
       } 
      }; 
    return View(key); 
} 

[View] 
@model Customer 
@Html.TextBoxFor(model=>model.Name) 

上面的代碼給我呈現爲

<input type="text" name="Name"/> 

的HTML,但我希望有

<input type="text" name="Customer.Name" /> 

如果我通過了AddDetail("Customer")行動。

我不知道如何獲得ViewContext並將Prefix附加到視圖。

有人可以幫我添加一個前綴嗎?

回答

3

試試這個:)

public ActionResult AddDetail(string Key) 
{ 
    ViewData.TemplateInfo.HtmlFieldPrefix = string.Format("{0}", key) 
    return View(key); 
} 
+0

非常感謝。它的工作就像一個魅力。 :-) – Sravan 2013-03-20 16:15:52

+0

歡迎您:) – karaxuna 2013-03-21 08:15:54