0

I am creating a dynamic user control in MVC 5 application. My view model looks like this:

public class StoredProceduerParametersRecordViewModel
{
    public string ProcedureName { get; set; }
    public string ParameterName { get; set; }
    public string ServerName { get; set; }
    public object ParameterValue { get; set; }
    //public object NewParameterName { get; set; }
}

The NewParameterName could be an int, string, bool or any data type.

I am getting the NewParameterName values from Stored Procedure Parameters

List<StoredProcedureParameter> parameters = new List<StoredProcedureParameter>();
parameters = database.StoredProcedures[procedure.ProcedureName]
                     .Parameters.OfType<StoredProcedureParameter>()
                     .ToList();

foreach (var param in parameters)
{
    StoredProceduerParametersRecordViewModel obj_parameter =
    new StoredProceduerParametersRecordViewModel();

    obj_parameter.ParameterName = param.Name;
    obj_parameter.NewParameterName = CommonHelper.ConvertSmoToClsType(param.DataType.SqlDataType, string.Empty);
    stored_procedure_parameters_list.Add(obj_parameter);
 }

My CommonHelper.ConvertSmoToClsType looks like this:

public static object ConvertSmoToClsType(SqlDataType dataType, string currentValue)
{
   object Object = new object();

   switch (dataType)
   {
       case SqlDataType.BigInt:
       Object = (String.IsNullOrEmpty(currentValue)) ? (Int64)0 : Int64.Parse(currentValue);
       break;
       case SqlDataType.Bit:
       Object = (String.IsNullOrEmpty(currentValue)) ? (Boolean)false : Boolean.Parse(currentValue);
       break;
       case SqlDataType.Char:
       case SqlDataType.VarChar:
       case SqlDataType.VarCharMax:
       Object = (String.IsNullOrEmpty(currentValue)) ? String.Empty : currentValue;
       break;
       case SqlDataType.Real:
       case SqlDataType.Numeric:
       case SqlDataType.Float:
       Object = (String.IsNullOrEmpty(currentValue)) ? 0D : Double.Parse(currentValue);
       break;
       and so on...
    }
}

I am trying to use HTML.Editor to display user controls

@foreach (var obj in Model.List_StoredProceduresParametersRecords_ViewModel)
{
  <tr id="@obj.ParameterName">
      <td class="parameterName" data-value="@obj.ParameterName">@obj.ParameterName</td>
      <td>@Html.Editor("NewParameterName")</td>
  </tr>
}

I referenced my code using this link ---> ASP.NET MVC3 Html.EditorFor and property of type object However, this solutions is not applicable for list.

How can I show the controls using Html.Editor or Html.EditorFor in my foreach loop?

2
  • I'm not actually seeing a question in this post? Just a bunch of code? Am I missing something? Commented Aug 1, 2014 at 20:30
  • The editor does not display the user control for that certain data type. What am I missing in my last code? Commented Aug 1, 2014 at 20:36

1 Answer 1

1

The EditorFor cannot ready empty strings so what I did was change this line of code.

Object = (String.IsNullOrEmpty(currentValue)) ? String.Empty : currentValue;

TO

Object = (String.IsNullOrEmpty(currentValue)) ? " " : currentValue;

I also changed my code from

<td>@Html.Editor("NewParameterName")</td>

TO

<td>@Html.EditorFor(model => obj.ParameterValue)</td>

It works now, except for my Double object. Do you have any ideas?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.