mardi 1 décembre 2015

mvc No data in ActionResult method after submit

I have an Index page on which there is a section to write a project name and select from a dropdownlist a project type.

Below that I have a submit button that directs to the ActionResult method Create in the Projects controller.

Code:

@using (Html.BeginForm("Create", "Projects"))
{
  @Html.AntiForgeryToken()

  <div class="top-spacing col-md-12 col-lg-12 col-sm-12">
    <div class=" well">
      @Html.ValidationSummary(true)
      <div class="row">
        @Html.LabelFor(model => model.Item1.projectName, htmlAttributes: new { @class = "control-label col-md-2 col-lg-2 col-sm-12" })
        <div class="col-md-10 col-lg-10 col-sm-12">
          @Html.TextBoxFor(model => model.Item1.projectName, htmlAttributes: new { @class = "ProjectNameInput" })
          @Html.ValidationMessageFor(model => model.Item1.projectName)
        </div>
      </div>
      @Html.ValidationSummary(true)
      <div class="row row-spacing">
        @Html.LabelFor(model => model.Item1.projectType, htmlAttributes: new { @class = "control-label col-md-2 col-lg-2 col-sm-12" })
        <div class="col-md-10 col-lg-10 col-sm-12">
          @Html.DropDownListFor(model => model.Item1.projectType, new  SelectList(
              new List<Object>{
                       new { value = 0 , text = "...Select..."  },
                       new { value = 1 , text = "Windows application"  },
                       new { value = 2 , text = "Web application" },
                       new { value = 3 , text = "Device application"}
                },
              "value",
              "text",
               0), htmlAttributes: new { @class = "DropDownList" })
          @Html.ValidationMessageFor(model => model.Item1.projectType)
        </div>
        <input type="hidden" value="" id="hdProjectID" />
      </div>
      <div class="row top-spacing col-md-offset-5 col-sm-offset-5">
        <div id="pnCreate" class=" col-sm-4 col-md-4 col-lg-4">
          <input type="submit" class="btn btn-default" value="Create" />
        </div>
        <div id="pnEdit" class=" col-sm-4 col-md-4 col-lg-4">

          <input type="submit" class="btn btn-default" value="Edit" />
          |
          <input type="submit" class="btn btn-default" value="Delete" />
        </div>
      </div>
    </div>
  </div>
}

ProjectsController:

 private myContext db = new myContext();

 // GET: Projects
 public ActionResult Index()
 {
   // allow more than one model to be used in the view
   return View(new Tuple<Project, Requirement, Priority>(new Project(), new Requirement(), new Priority()));
 }

 [HttpPost]
 [ValidateAntiForgeryToken]
 [AcceptVerbs(HttpVerbs.Post)]
 public ActionResult Create([Bind(Include = "projectName,projectType")] Project project)
 {
   if (ModelState.IsValid)
   {
     db.Project.Add(project);
     db.SaveChanges();
     return RedirectToAction("Index");
   }

   return RedirectToAction("Index");
 }

So when the submit button is clicked, the ActionResult Create is called, but the ModelState is not valid and does not have the information enterd by the user.

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire