0

Controller:

using (ISession session = NHIbernateSession.OpenSession())
    {                
      var item = session.Query<CarModel>().ToList();
      ViewBag.Modas = item;
      return View();
    }

View:

   <div class="form-group">
                @Html.LabelFor(model => model.Modelis.model_name, new { @class = "control-label col-md-2" })
                <div class="col-md-10">                    
                    @Html.DropDownListFor(model => model.Modelis.Id, new SelectList(ViewBag.Modas, "Id", "model_name"), "--Select Cars--")                       
                </div>
            </div>

Controller(submition part):

[HttpPost]
public ActionResult Create(Transport item)
{
    try
    {
        using (ISession session = NHIbernateSession.OpenSession())
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Save(item);
                transaction.Commit();
            }
        }

        return RedirectToAction("Index");
    }
    catch (Exception exception)
    {
        return View();
    }
}

And I get error on submit:

Value cannot be null. Parameter name: items

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.ArgumentNullException: Value cannot be null. Parameter name: items

And I don't understand what is wrong

user1883184
  • 106
  • 2
  • 11
  • Can you provide the controller method used for the submit. Specifically what does the method signature look like? What does the code look like that is throwing the exception? – andleer Jun 17 '14 at 14:10
  • 1
    You aren't specifying a model in your controller, so any reference to `model` is going to be `null`. – Nathan A Jun 17 '14 at 14:10

1 Answers1

0

With reference to your submitted code, it seems you are using Strongly Typed View of MVC. In this case you should supply model to your view. You are returning view without supplied or assigning a model. Hence your view has model as null. Please supply the model and check the execution.

using (ISession session = NHIbernateSession.OpenSession())
    {                
      var item = session.Query<CarModel>().ToList();
      ViewBag.Modas = item;
      // Either set the model like .... ViewData.Model = new YOUR_MODEL_CLASS();
     // OR return your model by view constructor like.. return View(new YOUR_MODEL_CLASS());
      return View();
    }
K D
  • 5,889
  • 1
  • 23
  • 35
  • How can I do it any of your suggestions? – user1883184 Jun 17 '14 at 14:18
  • Well if you are doing something like ... @Html.DropDownListFor(model => ...... then it requires a model to refer on the view page. You must have a strongly typed view. Hence you should intialize your model and assign it to the view from controller action method. – K D Jun 17 '14 at 14:20
  • If you don't know what is Strongly Typed view in MVC then refer this post .. http://stackoverflow.com/questions/2896544/what-is-strongly-typed-view-in-asp-net-mvc – K D Jun 17 '14 at 14:22
  • But if I remove DropDownListPart with model everything else works, @Html.LabelFor(model => model.lic_plate... and etc works. Or am I misunderstanding? – user1883184 Jun 17 '14 at 14:26
  • Html.LableFor will work fine because it doesn't requires instance of the model. It just access the property name by Type. You have to assign correct instance of model to the view – K D Jun 17 '14 at 14:43