0

我在我的模型(查看/共享/ EditorTemplates/List.cshtml)做了一個簡單的EditorTemplate的列表:的mvc EditorTemplate的列表<string>動態金額

@model List<string> 
foreach (var str in Model) 
{ 
    <li> 
     @Html.LabelFor(m => str, "My Label") 
     @Html.TextBoxFor(m => str) 
     @Html.ValidationMessageFor(m => str) 
    </li> 
} 

這樣調用在我看來(查看/ Profile.cshtml):

@using (Html.BeginRouteForm(MvcSettings.SitecoreRouteName, FormMethod.Post, new { data_abide = "true", id = "myForm", enctype = "multipart/form-data" })) 
{ 
    @Html.Sitecore().FormHandler("User", "UpdateProfile") 
    @Html.ValidationSummary() 

    @Html.EditorFor(x => x.ListTest, new { htmlAttributes = new { id = "listTestId" } }) 

    <input type="submit" value="Submit" /> 
} 

控制措施:

public ActionResult UpdateProfile(IntranetContactViewModel formModel) 
{ 
    // Save information to DB 
} 

型號:

public class IntranetContactViewModel 
{ 
    public List<string> ListTest { get; set; } 

    public IntranetContactViewModel() 
    { 
     ListTest = new List<string>{"abc","def","ghi"}; 
    } 
} 

當列表包含3個字符串時,視圖會呈現3個文本框。

<input class="text-box single-line" id="listTestId" name="ListTest[0]" type="text" value="abc"> 
<input class="text-box single-line" id="listTestId" name="ListTest[1]" type="text" value="def"> 
<input class="text-box single-line" id="listTestId" name="ListTest[2]" type="text" value="ghi"> 

但是,用戶可以插入的選項數量應該是無限的。如果所有3個文本框都填滿了第4個應該出現(並且,理想情況下,如果超過2個文本框爲空,則應刪除1個)。

我試圖通過添加一個文本框具有相同簽名這樣做我自己(加1 name屬性櫃檯裏面)

<input class="text-box single-line" id="listTestId" name="ListTest[3]" type="text" value="TEST"> 

但是,當我提出這一點,這沒有得到認可該模型並不返回給控制器。 有沒有辦法讓我的模型知道它現在還需要跟蹤這個新的文本框?或者這是不可能的?

請指教。

+0

我們可以看到更多的代碼形式定義和控制器函數接收它嗎?另外,你是否檢查過它肯定是通過檢查瀏覽器發送的POST內容來回傳的? – wizzardmr42

+0

該模型也可能有用,然後 – wizzardmr42

+0

進行編輯。看起來這個瀏覽器確實在新的領域有所收穫,並將其添加到POST中。 – Timon

回答

0
@Html.EditorFor(x => x.ListTest, new { htmlAttributes = new { id = "listTestId" } }) 

本來應該是:

@Html.EditorFor(x => x.ListTest) 

我強迫我所有的文本框,以獲得相同的ID,這搞砸了該系統。 感謝大家在評論中的幫助!