TextAreaFor ไม่ทำงานเมื่อใช้ HtmlAttributes Overload

ด้วยเหตุผลบางประการ เมื่อฉันตั้งค่าคุณสมบัติสไตล์ TextAreaFor โดยเฉพาะคุณสมบัติ width มันไม่ทำงาน สิ่งที่ฉันหมายถึงคือความกว้างไม่เปลี่ยนแปลง และ Id ก็ไม่เปลี่ยนแปลงเช่นกัน:

 @Html.TextAreaFor(model => model.AdminSetting.Style, new { htmlAttributes = new 
 { @class = "form-control", @style = "width: 100%;", @id = "HelloWorld" } })

แต่หากฉันลบคำว่า htmlAttributes และเปลี่ยนเป็นคำนี้ มันก็จะทำงานได้ดี ฉันสงสัยจริงๆว่าทำไม:

@Html.TextAreaFor(model => model.AdminSetting.Style, 
new { @class = "form-control", @style = "width: 100%;", @id = "HelloWorld" })

มีเหตุผลว่าทำไม TextAreaFor htmlAttributes ของฉันไม่ทำงานเว้นแต่ฉันจะลบการประกาศ htmlAttributes และประกาศเช่นนี้

 new { @class = "form-control", @style = "width: 100%;", @id = "HelloWorld" })

แทนสิ่งนี้?

new { htmlAttributes = new 
 { @class = "form-control", @style = "width: 100%;", @id = "HelloWorld" } })

ฉันได้ตรวจสอบเอกสารแล้ว และมั่นใจว่าฉันใช้โอเวอร์โหลดที่ถูกต้อง


person Willy David Jr    schedule 01.06.2018    source แหล่งที่มา
comment
htmlAttributes นี้จะใช้ได้กับ EditorFor โปรดดูความแตกต่าง   -  person Asif Raza    schedule 01.06.2018
comment
ใช่ ฉันเชื่อว่าสิ่งนี้จะได้ผล แต่ใช้ไม่ได้กับ TextAreaFor htmlAttributes คำนี้เป็นส่วนหนึ่งของเทมเพลตนั่งร้านที่สร้างจาก MVC5   -  person Willy David Jr    schedule 01.06.2018
comment
ใช่ มันใช้งานได้กับ EditorFor ฉันตรวจสอบแล้ว ฉันสงสัยว่าทำไมไม่อยู่ใน TextAreaFor   -  person Willy David Jr    schedule 01.06.2018


คำตอบ (2)


ลองตามที่แสดงด้านล่าง:

@Html.TextAreaFor(m => m.AdminSetting.Style, 
    new { maxlength = 1000, @class = "form-control", @id = "HelloWorld" })

ตัวช่วย Html.TextAreaFor, Html.EditorFor, Html.DisplayFor มีความพิเศษตรงที่เรียกกันว่า "ตัวช่วยแบบเทมเพลต" แทนที่จะแยก HTML บิตที่กำหนดไว้ออกไป พวกเขาสามารถใช้มุมมองเพื่อกำหนดว่าผลลัพธ์ของพวกเขาจะเป็นอย่างไร มุมมองเหล่านี้เรียกว่า "เทมเพลตตัวแก้ไข" หรือ "เทมเพลตดิสเพลย์" ตามลำดับ หากต้องการข้อมูลเพิ่มเติม โปรดดูที่ Html.EditorFor และ htmlAttributes

หวังว่านี่จะช่วยได้...

person Murat Yıldız    schedule 02.06.2018

ไวยากรณ์:

new { htmlAttributes = new { @class = "form-control", @style = "width: 100%;", @id = "HelloWorld" } })

แสดงถึงข้อมูลเพิ่มเติมสำหรับ ViewData และสามารถใช้กับ EditorFor

ดู: MVC View htmlAttribute not work

__System_Object_" rel = "nofollow noreferrer" ชื่อ ="EditorFor">MSDN - EditorFor

__System_Object_" rel="nofollow noreferrer"> MSDN - TextAreaFor

person Gecko    schedule 01.06.2018