2012-10-15 44 views
7

我在下面的文本框中我ViewASP.NET MVC TextBoxFor佔位

enter image description here

的佔位符代碼:

@Html.TextBoxFor(m => m.Listing.Location.AddressLine1, new {@placeholder="Address Line 1 - Required"}) 

問題:如何讓文字RED中的「必需」。

+0

你需要爲它創建所需的模型實體和使用.. – Umitk

回答

4

你不能用文本字段來做到這一點。您無法控制HTML5 placeholder屬性的某些部分的格式。更何況,如果你想實現多色文本,你不能再使用文本字段或textarea。您將必須使用具有contenteditable="true"屬性的元素並編寫自定義插件。

這裏例如標記可能什麼樣子:

<div contenteditable="true"> 
    Address Line 1 - <span style="color: red">Required</span>; 
</div> 

但由於這是一個div元素,你現在有一個對應的隱藏字段來支持它,這樣的價值被髮送到服務器。此外,當此字段的值發生更改時,您還需要重新同步隱藏的字段值。這將是很多工作。

+1

這不完全是真的,你可以使用css來設置佔位符的樣式(在某些瀏覽器中)http://stackoverflow.com/questions/2610497/change-an-inputs-html5-placeholder-color-with-css ... – nemesv

+1

@nemesv,沒錯,但他想要只佔據佔位符文本的一部分。我已經更新了我的答案來解決這個問題。 –

+0

嗯。我想我對於我來說工作太多了。 – Subby

1
模型

[必需]

public string AddressLine1{ get; set; } 

@Html.TextBoxFor(model => model.Listing.Location.AddressLine1, new { @class = "form-control" , placeholder = "Address Line 1 - Required" }) 

嘗試一下本作空@ Html.TextBox

@Html.TextBox("CustomarName" ,null, new { @class = "form-control" , @placeholder = "Search With Customar Name" })