我正在动态创建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>