如何在HttpPost上获取动态创建的文本框的值

时间:2015-04-21 13:27:19

标签: c# asp.net asp.net-mvc asp.net-mvc-3 c#-4.0

我正在动态创建Textbox控件如下,但在HttpPost上它没有返回任何内容。我希望可以在HttpPost的Controller中访问文本框的值。任何人都可以建议我如何实现这一目标。感谢

模型

public class MyViewModel
{
    public ControlViewModel[] Controls { get; set; }
}

public abstract class ControlViewModel
{
    public abstract string Type { get; }
    public bool Visible { get; set; }
    public string Label { get; set; }
    public string Name { get; set; }
}

public class TextBoxViewModel : ControlViewModel
{
    public override string Type
    {
        get { return "textbox"; }
    }
    public string Value { get; set; }
}

控制器

public ActionResult Index(Guid? id)
{
    return Results(id);
}

[HttpPost]
public ActionResult Index(MyViewModel model)
{
    // Logic here
}

查看

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/Site.master"
 Inherits="System.Web.Mvc.ViewPage<MySite.Model.ViewModels.MyViewModel>" %>

<div>
    <% using (Html.BeginForm("Index", "MyController", FormMethod.Post, null))
    { %>
        <% for (int i = 0; i < Model.Controls.Length; i++)
           { %>
               <%Html.RenderPartial("TextboxControl", (TextBoxViewModel)Model.Controls[i]); %>             
        <% } %>
        <input type="submit" value="Submit" class="btn btn-primary"/>       
    <% } %>
   </div>

用户控件

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MySite.Model.ViewModels.TextBoxViewModel>" %>
<div>
     <%
           var controlType = Model.Type;
           var controlName = Model.Name;               
     %>
     <%= Html.TextBoxFor(x => x.Value, new { id = controlName, type = controlType, @class = "input-medium" })%>
</div>

1 个答案:

答案 0 :(得分:-2)

我认为你应该使用FindControl()..

查看Getting values from dynamic textboxes

相关问题