I have created a MVC 5 application that generates dynamic controls. The formcollection is null on postback. I have enclosed the form in formtag. Not sure what the problem is
Below is the main form. Clciking on submit doesnt post back formcollection
Actual UI
View containing the menu items
@model IEnumerable<CC.GRP.BluePrismMenu.Models.BluePrismRequestCustomer>
@using Kendo.Mvc.UI;
@using (Html.BeginForm("MenuResult", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@*@Html.AntiForgeryToken()*@
<div class="sidebar-nav k-content">
@(Html.Kendo().Menu()
.Name("nav")
.BindTo(Model, mappings => {
mappings.For<CC.GRP.BluePrismMenu.Models.BluePrismRequestCustomer>
(binding => binding
.ItemDataBound((item, requestCustomer) => {
if (requestCustomer.Active == true) {
item.Text = requestCustomer.CustomerDisplayName;
}
})
.Children(requestCustomer => requestCustomer.BluePrismRequestTypes));
mappings.For<CC.GRP.BluePrismMenu.Models.BluePrismRequestType>
(binding => binding
.ItemDataBound((item, requestType) => {
if (requestType.CustomerID > 0) {
if (requestType.Active == true) {
item.Text = Convert.ToString(requestType.RequestTypeID) + "," + requestType.RequestTypeValue;
}
}
}));
})
.Orientation(MenuOrientation.Vertical)
.Events(e => e.Select("menuItemSelect"))
)
</div>
}
<script type="text/javascript">
var Cid = "";
function menuItemSelect(e) {
var Cname = "";
var MSelect = $(e.item).children(".k-link").text();
var fields = MSelect.split(",");
if (fields[0] != null) {
Cid = fields[0];
Cname = fields[1];
}
$("#article").load("Home/MenuResult", { requestTypeID: Cid });
}
//function onClickPost() {
// $("#article").load("Home/Edit", { requestTypeID: Cid });
//}
</script>
<div id="article" class="article-div">
</div>
View containing the dynamic controls
@model IEnumerable<CC.GRP.BluePrismMenu.Models.BluePrismRequestTypeConfig>
@{
ViewBag.Title = "Blue Prism Submit Edit";
}
<hgroup class="title">
<h1>@ViewBag.Title.</h1>
<h2>@ViewBag.Message</h2>
</hgroup>
<BR />
@using (Html.BeginForm("Edit", "Home"))
{
<script type="text/javascript">
function onClickPost() {
$("#article").load("Home/Edit", {requestTypeID: Cid});
}
</script>
@Html.AntiForgeryToken()
<div class="form-horizontal" id="myform">
<div class="row">
<h4>Send Request @ViewBag.Header</h4>
</div>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<fieldset>
<legend>Edit</legend>
@foreach (var item in Model)
{
switch (item.FieldType)
{
case "TEXT":
<div class="form-group row">
@Html.Label(item.FieldLable, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@(Html.Kendo().TextBox().Name(item.FieldLable.Replace(" ", "-")))
@Html.ValidationMessage(item.FieldLable.Replace(" ", "-"), new { @class = "text-danger" })
</div>
</div>
break;
case "DATE":
<div class="form-group row">
@Html.Label(item.FieldLable, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@(Html.Kendo().DatePicker().Name(item.FieldLable.Replace(" ", "-")).Format("dd/MM/yyyy"))
@Html.ValidationMessage(item.FieldLable.Replace(" ", "-"), new { @class = "text-danger" })
</div>
</div>
break;
case "NUMBER":
<div class="form-group row">
@Html.Label(item.FieldLable, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@(Html.Kendo().NumericTextBox().Name(item.FieldLable.Replace(" ", "-")).Format("{0:n0}").Decimals(0))
@Html.ValidationMessage(item.FieldLable.Replace(" ", "-"), new { @class = "text-danger" })
</div>
</div>
break;
}
}
</fieldset>
</div>
<br /><br />
<div class="form-group row">
<div class="col-md-offset-2 col-md-10">
@(Html.Kendo().Button()
.HtmlAttributes(new { type = "button" })
.Name("textButton")
.Icon("tick")
.Content("Submit")
.Events(ev => ev.Click("onClickPost"))
)
<a href="/CC.GRP.BluePrismMenu/" id="cancel">Clear</a>
<script>jQuery(function () { jQuery("#cancel").kendoButton({ "icon": "cancel" }); });</script>
</div>
</div>
}
Controller
[AcceptVerbs(HttpVerbs.Post)]
//[ValidateAntiForgeryToken]
public ActionResult Edit(int? RequestTypeID, FormCollection fc)
{
ServiceReference1.ComputaCenterLoader1PortTypeClient Service = new ServiceReference1.ComputaCenterLoader1PortTypeClient();
Service.ClientCredentials.UserName.UserName = ConfigurationManager.AppSettings.Get("userName");
Service.ClientCredentials.UserName.Password = ConfigurationManager.AppSettings.Get("Password");
//var bluePrismRequestTypeConfig = db.BluePrismRequestTypeConfigs.Where(o => o.RequestTypeID == RequestTypeID && o.Active == true).ToList();
BluePrismRequestType bluePrismRequestType = db.BluePrismRequestTypes.Find(RequestTypeID);
StringBuilder builder = new StringBuilder();
int count;
builder.Length = 0;
count = 0;
foreach (var key in fc.Keys)
{
if (key.ToString() != "__RequestVerificationToken")
{
count = count + 1;
if (count == 1)
{
builder.Append(fc[key.ToString()]);
}
else
{
builder.Append("," + fc[key.ToString()]);
}
}
}
try
{
Service.Loader("auto", bluePrismRequestType.RequestTypeValue.ToString(), builder.ToString());
TempData["Status"] = "Send successfully";
}
catch (SoapException error)
{
TempData["Status"] = error.Detail.ToString();
}
catch (Exception e)
{
TempData["Status"] = e.InnerException.Message.ToString();
}
BluePrismTraceMessage bluePrismTraceMessage = new BluePrismTraceMessage();
bluePrismTraceMessage.TraceTime = DateTime.Now;
bluePrismTraceMessage.USERID = WindowsIdentity.GetCurrent().Name;
bluePrismTraceMessage.Message = "RequestType:" + bluePrismRequestType.RequestTypeValue + ";variable:" + builder.ToString();
bluePrismTraceMessage.ResponseXML = TempData["Status"].ToString();
db.BluePrismTraceMessages.Add(bluePrismTraceMessage);
db.SaveChanges();
Service.Close();
return RedirectToAction("Index");
}