2

我只是为我的一个项目测试 ASP.NET MVC Razor,我遇到了一个我目前没有解决方案的问题。也许有人可以帮助我。我有一个包含两个输入元素的页面。一个文本框和一个由模型表填充的下拉列表。现在我希望能够克隆该字段,以便用户可以进行更多输入,然后只输入一行。我寻找了不同的方法,但我总是丢失 DropDownList 的原因。我不太适合 js,所以也许我错过了绑定下拉列表的好方法。这是我的页面:

@{
    ViewBag.Title = "SelectionList";
}
<h1>Search for Models</h1>
<h2>Search Request</h2>
<fieldset>
<legend>Here you can Search for all accessible TumorModels</legend>
@using(Html.BeginForm("SearchTumorModel","Search",FormMethod.Post, new { @class = "searchForm"}))
{
    <table id="formTable"> 
        <thead>
            <tr>
                <th>Specification</th>
                <th>Input</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td> @Html.TextBox("Search") in</td>
                <td> @Html.DropDownList("SelectionList", (IEnumerable<SelectListItem>) ViewBag.SelectionList) </td>
            </tr>
        </tbody>
    </table> 
    <p> <button name="button" value="add" class="formbutton" >Add one more search field</button></p>
    <p> 
        <button name="button" value="search" class="formbutton" >Search</button>
        <button name="button" value="clear" class="formbutton" >Clear</button>
    </p>
}
</fieldset>

感谢帮助 :)

更新:解决:

@model Tumormodelle.Models.SearchModel
<table>
  @for (int i = 0; i < (int) Model.SearchCount ;i++ )
{  <tr>
    <th>@if(i > 0){<select name="ConnectorList" id="ConnectorList">
        <option value="AND" @(Model.ConnectorList.ElementAt(i-1).Equals("AND") ?"selected":"")>AND</option>
        <option value="OR" @(Model.ConnectorList.ElementAt(i-1).Equals("OR") ?"selected":"") >OR</option> 
         <option value="NAND" @(Model.ConnectorList.ElementAt(i-1).Equals("NAND") ?"selected":"") >NAND</option>
         <option value="NOR" @(Model.ConnectorList.ElementAt(i-1).Equals("NOR") ?"selected":"") >NOR</option>
                   </select>}</th>
 <th> <input name="SearchInput" id="SearchInput" type="text" value="@(Model.SearchList.ElementAt(i) as String)" /></th>
<th><select name="SelectionList" id="SelectionList">
@for (int j = 0; j < Model.SelectedList.Count(); j++)
{<option value="@j" @(Model.Selection.ElementAt(i).Equals(j) ?"selected":"")> @(Model.SelectedList.ElementAt(j).Name as String)</option> 
}    </select> 
</th></tr>}

</table><p> <button name="button" value="add" class="formbutton" >Add one more search field</button> <button name="button" value="sub" class="formbutton" >Remove one search field</button></p>
4

1 回答 1

2

史蒂文·桑德森(Steven Sanderson)excellent article逐步解释了如何实现这一目标。这个想法是有一个控制器动作,它将返回一个包含新记录的局部视图,当用户决定添加一个新行时,它将使用 AJAX 调用来调用。它还使用了一个名为的自定义助手Html.BeginCollectionItem,它为输入字段的名称生成非顺序索引(Guid),以便standard naming convention在添加/删除行时得到尊重。

于 2013-05-17T09:13:25.790 回答