Rubbertjuh
Rubbertjuh

Reputation: 145

MVC Model returns null to controller after Form POST

My Model (CountryContinentModels) is always null after posting a form. In my controller I always get model null and I can't seem to find why, I've looked through the code several times, asked around with my class mates and checked several other stack overflow questions & answers, most of the times the issue is related to naming, I don't think this is the case here. I'm trying to Create a new entry in my database, using the CountryContinent Model (has Country and Continent)

I'm setting up a CRUD, and I'm getting stuck setting up the Create.

Here is the Create view:

    @model TransactionImporter.WebUI.Models.CountryContinentModels

    @{
    ViewBag.Title = "Create";
    }

    <h2>Create</h2>


    @using (Html.BeginForm()) 
    {
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>CountryContinentModels</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Country, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Country, new { htmlAttributes = new { @class = "form-control" } })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Continent, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Continent, new { htmlAttributes = new { @class = "form-control" } })
            </div>
        </div>
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default"/>
            </div>
        </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Here is the Controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TransactionImporter.BLL.Interfaces;
using TransactionImporter.Factory;
using TransactionImporter.WebUI.Models;
using TransactionImpoter.Domain;

namespace TransactionImporter.WebUI.Controllers
{
    public class CountryContinentController : Controller
    {
        private ICountryContinentLogic countryContinentLogic = CountryContinentFactory.CreateLogic();

        // GET: CountryContinent/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: CountryContinent/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(CountryContinentModels models)
        {
            if (ModelState.IsValid)
            {
                CountryContinent countryContinent = new CountryContinent(models.Country, models.Continent);
                countryContinentLogic.AddCountry(countryContinent);

                return RedirectToAction("Index");
            }
            else
            {
                return View();
            }
        }

        // GET: CountryContinent
        public ActionResult Index()
        {
            List<CountryContinent> countryContinents = countryContinentLogic.GetAllCountries();
            List<CountryContinentModels> countryModels = new List<CountryContinentModels>();
            foreach (CountryContinent country in countryContinents)
            {
                countryModels.Add(new CountryContinentModels(country.Country, country.Continent));
            }

            return View(countryModels);
        }


        // GET: CountryContinent/Edit/5
        public ActionResult Edit(int id)
        {
            CountryContinent country = new CountryContinent(countryContinentLogic.GetCountryById(id).Country,
                countryContinentLogic.GetCountryById(id).Continent);
            CountryContinentModels model = new CountryContinentModels(country.Country, country.Continent);
            return View(model);
        }

        // POST: CountryContinent/Edit/5
        [HttpPost]
        public ActionResult Edit(CountryContinentModels model, int id, FormCollection collection)
        {
            try
            {
                CountryContinent continent = new CountryContinent(model.Country, model.Continent);
                countryContinentLogic.UpdateCountryById(id, continent);

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

        // GET: CountryContinent/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: CountryContinent/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

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

Here is the Model:

using System;
using System.Collections;
using System.ComponentModel.DataAnnotations;

namespace TransactionImporter.WebUI.Models
{
    public class CountryContinentModels
    {
        public CountryContinentModels(string country, string continent)
        {
            Country = country;
            Continent = continent;
        }
        public CountryContinentModels() { }
        public int Id { get; private set; }
        [Display(Name = "Country")]
        public string Country { get; private set; }
        [Display(Name = "Continent")]
        public string Continent { get; private set; }

        public IEnumerator GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }
}

Upvotes: 0

Views: 3030

Answers (1)

Will Eddins
Will Eddins

Reputation: 13917

When the model-binding happens, it helps to understand the steps that ASP.NET takes to create the model, specifically:

  1. ASP.NET creates an instance of your model using the default constructor.
  2. Each property's set method is called if there is an HTML input with a matching name.

On first look, you'll need to change your properties to have public set methods like so:

public string Continent { get; set; }

That should fix the issue, but if it doesn't, you can check the generated names in the HTML to make sure they make sense relative to the names of your C# properties.

Upvotes: 4

Related Questions