2

Am trying to find out why the system giving me this error in visual studio ",i populate the dropdown from the database and it has values at runtime, but when i click the submit button i get the above error, wondering what am i doing wrong here. here is my code // this is the model

public class LeaveRequest
{
    public string TableTypecode { get; set; };
    public string TableTypeName { get; set; };

    [Display(Name = "Selected TypeCode")]
    public int SelectedTableTypeCode { get; set; }
    public IEnumerable<SelectListItem> LoadedTableTypeName { get; set; }
}   

And below is how am fetching the data from sqlserver .. works well.

  public class OpenTypeList
    {

         public List<LeaveRequest> OpenList()
         {
             string hasconnections = 
                  ConfigurationManager.ConnectionStrings["leaveConnections"]
                 .ConnectionString;

    List<LeaveRequest> P2 = new List<LeaveRequest>();

    using (SqlConnection con = new SqlConnection(hasconnections))
    using (SqlCommand cmd = new SqlCommand("ViewTypes", con))
    {
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@ListName", "status");
        con.Open();
        using (SqlDataReader dr = cmd.ExecuteReader())
        {
            while (dr.Read())
            {
                LeaveRequest pom = new LeaveRequest();
                pom.TableTypecode = Convert.ToString(dr.GetValue(0));
                pom.TableTypeName = dr.GetString(1);
                P2.Add(pom);
               }
            }
              return P2;
        }


     }
}

so here i populate the list from my controller

    [HttpGet]
    public ActionResult LeaveRequestIndex(LeaveRequest pleave)
    {
        var modal = new LeaveRequest()
        {
            UserRoles = GetRoles(),
            LoadedTableTypeName = GetTypesIdz()
        };
        return View(modal);
    }   

And lastly below is my razor code .. in the view

  <div class="form-group">
    @Html.LabelFor(model => model.RequestStatus)
    @Html.DropDownListFor(model => model.SelectedTableTypeCode, 
    Model.LoadedTableTypeName, new { @class = "form-control" })
  </div>

This error is generated when i click the submit button.
my post method is here...

    [HttpPost]

    public ActionResult LeaveRequestIndex(FormCollection fmcollection)
    {
        foreach (string key in fmcollection.AllKeys)
        {
            Response.Write("Key= " + key + "  ");
            Response.Write(fmcollection[key]);
            Response.Write("<br/>");

          }
        return View();
      }
Bels
  • 85
  • 8
  • So you are saying this error you are getting on submit. So can you show us your post method – Power Star Aug 09 '17 at 10:09
  • been trying to see the keys each control on the form would return. but when i remove the dropdown list code it works – Bels Aug 09 '17 at 10:20
  • Because you do not repopulate the `LoadedTableTypeName` property in the POST method before you return the view so its `null` –  Aug 09 '17 at 13:09

1 Answers1

1

You need to bind your models in post method also. So update your post method like below.

[HttpPost]
public ActionResult LeaveRequestIndex(FormCollection fmcollection)
{
    foreach (string key in fmcollection.AllKeys)
    {
        Response.Write("Key= " + key + "  ");
        Response.Write(fmcollection[key]);
        Response.Write("<br/>");

    }

    var modal = new LeaveRequest()
        {
            UserRoles = GetRoles(),
            LoadedTableTypeName = GetTypesIdz()
        };
        return View(modal);
  }
Power Star
  • 1,724
  • 2
  • 13
  • 25