Моя модель (CountryContinentModels) всегда пуста после публикации формы.В моем контроллере я всегда получаю нулевую модель, и я не могу понять, почему, я несколько раз просматривал код, расспрашивал своих одноклассников и проверял несколько других вопросов и ответов о переполнении стека, в большинстве случаев проблемасвязано с именами, я не думаю, что это так.Я пытаюсь создать новую запись в моей базе данных, используя модель CountryContinent (имеет страну и континент)
Я настраиваю CRUD, и я застреваю, настраивая Create.
Вот вид Create:
@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")
}
Вот контроллер:
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();
}
}
}
}
Вот модель:
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();
}
}
}