C#mvc5通过控制器添加文本框

时间:2018-06-25 14:32:53

标签: c# asp.net-mvc view

是否可以通过控制器向视图添加texbox?

我有动态数量的文本框,需要一种方法将控制器添加到视图中。这可能吗?

1 个答案:

答案 0 :(得分:0)

      Do Something like this. Just a quick written code. Might be syntax errors.

      DynamicTextBoxCreation.cs    
      public class DynamicTextBoxCreation
        {
            public List<string> textBoxList;

            public ActionResult DynamicTextBox()
            {
               /* Fill your dynamic list here from DB or from any other logic */
               return textBoxList;
            }
        }

        DynamicTextBox.cshtml            
        @model DynamicTextBoxCreation;
        @code{
          var i = 1;
        }

        foreach(var tbItem in textBoxList)
        {
            @Html.TextBox(' + tbItem + i + ', "", new {@class = "css-class", @onclick = "alert('demo');"});
            i += 1;
        }