繁体   English   中英

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

[英]How to get the value of dynamically created textbox on HttpPost

我正在按以下方式动态创建Textbox控件,但是在HttpPost它没有返回任何内容。 我期望在HttpPost Controller中可以访问textbox的值。 谁能建议我如何实现这一目标。 谢谢

楷模

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>

我认为您应该使用FindControl()。

看一下从动态文本框中获取值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM