[英]How to get the value of dynamically created textbox on HttpPost
I am creating Textbox
control Dynamically as following, but on HttpPost
it is not returning anything back. 我正在按以下方式动态创建Textbox
控件,但是在HttpPost
它没有返回任何内容。 I am expecting the value of textbox to be accessible in Controller on HttpPost
. 我期望在HttpPost
Controller中可以访问textbox的值。 Can anyone please suggest me how I can achieve this. 谁能建议我如何实现这一目标。 Thanks 谢谢
Models 楷模
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; }
}
Controller 控制者
public ActionResult Index(Guid? id)
{
return Results(id);
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
// Logic here
}
View 视图
<%@ 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>
UserControl 用户控件
<%@ 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>
I think you should use FindControl().. 我认为您应该使用FindControl()。
Take a look on Getting values from dynamic textboxes 看一下从动态文本框中获取值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.