I am creating Textbox
control Dynamically as following, but on HttpPost
it is not returning anything back. I am expecting the value of textbox to be accessible in Controller on HttpPost
. 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>