Hi, I am trying to pass list of object attributes from a cshtml to Httpost that saves the info to the Database sql 2008.
See the code below.
I keep getting error :
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: Input string was not in a correct format.
=======================================================================================PPLEASE HELP!
Debugged and found that the value for SurveyID is not being posted to the controller from the action
=====================================================================
Please let me know if the syntax is right.
Custom model binder:
public class SurveysCustomBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
SurveyItems model = (SurveyItems)bindingContext.Model ?? (SurveyItems)DependencyResolver.Current.GetService(typeof(SurveyItems));
bool hasPrefix = bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName);
string searchPrefix = (hasPrefix) ? bindingContext.ModelName + "." : "";
model.SurveyID = int.Parse(GetValue(bindingContext, searchPrefix, "SurveyID"));
model.SurveyItemID = int.Parse(GetValue(bindingContext, searchPrefix, "SurveyItemID"));
model.SurveyResultsID = int.Parse(GetValue(bindingContext, searchPrefix, "SurveyResultsID"));
model.SurveyItemCount = int.Parse(GetValue(bindingContext, searchPrefix, "SurveyItemCount"));
model.SurveyUserID = GetValue(bindingContext, searchPrefix, "SurveyUserID");
return new SurveyItems
{
SurveyID = model.SurveyID,
SurveyItemID = model.SurveyItemID,
SurveyResultsID = model.SurveyResultsID,
SurveyItemCount = model.SurveyItemCount,
SurveyUserID = model.SurveyUserID
};
}
private string GetValue(ModelBindingContext context, string prefix, string key)
{
ValueProviderResult vpr = context.ValueProvider.GetValue(prefix + key);
return vpr == null ? null : vpr.AttemptedValue;
}
private bool GetCheckedValue(ModelBindingContext context, string prefix, string key)
{
bool result = false;
ValueProviderResult vpr = context.ValueProvider.GetValue(prefix + key);
if (vpr != null)
{
result = (bool)vpr.ConvertTo(typeof(bool));
}
return result;
}
}
Model Object class:
public class SurveyItems
{
public int SurveyID { get; set; }
public int SurveyItemID { get; set; }
public int SurveyResultsID { get; set; }
[Required(ErrorMessage = "Digits only except a negative sign is allowed")]
[Range(0, int.MaxValue)]
public int? SurveyItemCount { get; set; }
public string SurveyQuestion { get; set; }
public string SurveyInstructions { get; set; }
public string Sitems { get; set; }
public string SurveyStatus { get; set; }
public string SurveyUserID { get; set; }
}
================
razor file:
@model IList<BusinessLayer.SurveyItems>
@using TDMIntranet.Models
@{
ViewBag.Title="Please edit the item count for the Survey";
}
@using (Html.BeginForm("EditSurveyBySurveyIDandCDS", "CorporateSurveyRequests", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
if (Model.Count > 0)
{
for (int i = 0; i < Model.Count; i++)
{
<div class="editor-label">
@Html.TextBoxFor(ModelItem => Model[i].SurveyItemCount, new{SurveyID = Model[i].SurveyID,SurveyItemID = Model[i].SurveyItemID})
@Html.DisplayFor(ModelItem => Model[i].Sitems)
</div>
@Html.DisplayFor(ModelItem => Model[i].SurveyID)<br />
@Html.DisplayFor(ModelItem => Model[i].SurveyItemID) <br />
@Html.DisplayFor(ModelItem => Model[i].SurveyResultsID) <br />
@Html.DisplayFor(ModelItem => Model[i].SurveyUserID)
@Html.HiddenFor(ModelItem => Model[i].SurveyResultsID)
@Html.HiddenFor(ModelItem => Model[i].SurveyItemCount)
@Html.HiddenFor(ModelItem => Model[i].SurveyUserID)
@Html.HiddenFor(ModelItem => Model[i].SurveyID)
@Html.HiddenFor(ModelItem => Model[i].SurveyItemID)
}
}
<p>
<input type="submit" value="Save" />
</p>
========
HTTPPost controller action
======
[HttpPost]
public ActionResult EditSurveyBySurveyIDandCDS(IList<SurveyItems> inputitems)
{
try
{
if (ModelState.IsValid)
{
for (int i = 0; i < inputitems.Count; i++)
{
foreach (var s in inputitems)
{
if (s.SurveyItemCount >= 0)
{
SurveyItems sitems = new SurveyItems();
sitems.SurveyID = s.SurveyID;
sitems.SurveyItemID = s.SurveyItemID;
sitems.SurveyResultsID = s.SurveyResultsID;
sitems.SurveyItemCount = s.SurveyItemCount;
string CDS = System.Web.HttpContext.Current.Session["CDSIDValue"].ToString().ToLower();
sitems.SurveyUserID = CDS;
SurveysBusinessLayer requestsBusinessLayer = new SurveysBusinessLayer();
requestsBusinessLayer.AddSurveyResults(sitems);
return RedirectToAction("ThanksforTakingTheSurvey");
}
}
}
}
else
{
TempData["notice"] = "Please enter a valid integer value for the Item count.";
return RedirectToAction("EditSurveyResultsError");
}
}
catch (Exception ex)
{
TempData["Notice"] = ex;
}
return View();
}
.....
How do i pass SurveyID and SurveyItemID in the razor view for each survey Item Count :
@Html.TextBoxFor(ModelItem => Model[i].SurveyItemCount, new{SurveyID = Model[i].SurveyID,SurveyItemID = Model[i].SurveyItemID})
????? Is this correct???