发布表单后,我的模型(CountryContinentModels)始终为null。 在我的控制器中,我总是得到模型null,我似乎无法找到原因,我已经多次查看了代码,询问了我的班级伙伴并检查了其他几个堆栈溢出问题&答案,大多数时候问题与命名有关,我不认为这是这种情况。 我正在尝试使用CountryContinent模型(包含Country and Continent)在我的数据库中创建一个新条目
我正在设置一个CRUD,而且我很难设置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();
}
}
}
答案 0 :(得分:4)
当模型绑定发生时,它有助于理解ASP.NET创建模型所采取的步骤,具体来说:
set
的HTML输入,则会调用每个属性的name
方法。首先,您需要更改属性以使用公共set
方法,如下所示:
public string Continent { get; set; }
这应该可以解决问题,但如果没有,您可以检查HTML中生成的名称,以确保它们与C#属性的名称相关。