对象引用没有设置为Razor下拉列表中的对象实例[duplicate]

时间:2022-12-15 16:54:20

This question already has an answer here:

这个问题已经有了答案:

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

我从数据库中填充下拉菜单,它在运行时具有值,但是当我单击submit按钮时,我得到了上面的错误,我想知道我在这里做错了什么。这是我的代码//这是模型

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.

下面是如何从sqlserver获取数据。工作得很好。

  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...

当我单击submit按钮时,将生成此错误。我的post方法在这里……

    [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();
      }

1 个解决方案

#1


1  

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

您还需要在post方法中绑定模型。所以更新你的post方法如下。

[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);
  }

#1


1  

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

您还需要在post方法中绑定模型。所以更新你的post方法如下。

[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);
  }