c# - 表单 POST 后 MVC 模型向控制器返回 null
问题描述
发布表单后,我的模型 (CountryContinentModels) 始终为空。在我的控制器中,我总是得到模型 null 并且我似乎找不到原因,我已经多次查看代码,与我的同学一起询问并检查了其他几个堆栈溢出问题和答案,大多数时候是问题与命名有关,我认为这里不是这种情况。我正在尝试使用 CountryContinent 模型(具有 Country 和 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();
}
}
}
解决方案
当模型绑定发生时,有助于理解 ASP.NET 创建模型所采取的步骤,特别是:
- ASP.NET 使用默认构造函数创建模型的实例。
set
如果有一个匹配的 HTML 输入,则调用每个属性的方法name
。
乍一看,您需要将属性更改为具有如下公共set
方法:
public string Continent { get; set; }
这应该可以解决问题,但如果没有,您可以检查 HTML 中生成的名称,以确保它们相对于 C# 属性的名称有意义。
推荐阅读
- java - 这是 http://www.springframework.org/schema/util 的 Spring JAR
- math - 涉及多概率的蒙特卡罗模拟
- c++ - 在游戏上下文中更新 actor 属性
- android - 带有图像上传百分比的 Android 进度条
- typescript - 如何在 IntelliJ IDEA 中修复 TypeScript 的 Mocha 全局符号?
- r - R中一个数据帧中的模糊匹配
- ios - 应用内购买的自动更新选项不可见
- python - Python - 在表格中水平插入列表
- android - 如何验证 Raspberry 和 Android 之间的蓝牙通信
- angular - 数据绑定在角度不起作用