ModelState.IsValid的问题始终为true,并在提交时插入
时出错她是我的ViewModel(CompaniesViewModel.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace kambi4.Models
{
public class CompaniesViewModel
{
[Required(ErrorMessage = "A Company Name is required")]
[Display(Name = "Company Name: ")]
public string Company { get; set; }
[Required(ErrorMessage = "A Company Legal ID is required")]
[Display(Name = "Company Legal ID: ")]
public string LegalId { get; set; }
[Required(ErrorMessage = "A Contact Email is required")]
[DataType(DataType.EmailAddress, ErrorMessage = "Please insert a valid Email")]
[Display(Name = "Contact Email: ")]
public string ContactEmail { get; set; }
[Required(ErrorMessage = "A Contact Name is required")]
[Display(Name = "Contact Name: ")]
public string ContactName { get; set; }
[Required(ErrorMessage = "A Contact Phone is required")]
[Display(Name = "Contact Phone: ")]
public string ContactPhone { get; set; }
}
}
这是控制器(或控制器的功能,我有问题)(CompanyController.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.ComponentModel.DataAnnotations;
namespace kambi4.Controllers
{
public class CompanyController : Controller
{
//
// GET: /Company/
Models.EADBDataContext dc = new Models.EADBDataContext();
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult RegisterCompany()
{
return View();
}
[HttpPost]
public ActionResult RegisterCompany(Models.CompaniesViewModel company)
{
if (ModelState.IsValid) //<- Is always true here
{
var a = new Models.companies();
a.company = company.Company;
a.companycode = (new Guid()).ToString();
a.email = company.ContactEmail;
a.contactname = company.ContactName;
a.contactphone = company.ContactPhone;
a.legalid = company.LegalId;
a.RegisterDate = DateTime.Now;
a.status = 3;
dc.companies.InsertOnSubmit(a);
dc.SubmitChanges();
}
else
{
var errors = ModelState.Values.SelectMany(v => v.Errors);
ModelState.AddModelError("", "Save Failed. Check the fields for errors");
}
return View(company);
}
[HttpGet]
public ActionResult RegisterClient()
{
return View();
}
[HttpPost]
public ActionResult RegisterClient(Models.ClientModel Client)
{
if (ModelState.IsValid)
{
}
else
{
ModelState.AddModelError("", "Save Failed. Check the fields for errors");
}
return View();
}
}
}
最后但并非最不重要的是我的View(RegisterCompany.cshtml)
@model kambi4.Models.CompaniesViewModel
@{
ViewBag.Title = "Register as Service Company";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
@Html.ValidationSummary(true, "Create Company Failed. Check your fields");
<div>
<fieldset>
<legend>Register as Service Company</legend>
<table id="RegisterCompany">
<tr>
<td align="right">@Html.LabelFor(u => u.Company)</td>
<td align="left">@Html.TextBoxFor(u => u.Company, new { @class = "k-input" })</td>
<td align="left"><span class="error">@Html.ValidationMessageFor(u => u.Company)</span></td>
</tr>
<tr>
<td align="right">@Html.LabelFor(u => u.LegalId)</td>
<td align="left">@Html.TextBoxFor(u => u.LegalId, new { @class = "k-input" })</td>
<td align="left"><span class="error">@Html.ValidationMessageFor(u => u.LegalId)</span></td>
</tr>
<tr>
<td align="right">@Html.LabelFor(u => u.ContactName)</td>
<td align="left">@Html.TextBoxFor(u => u.ContactName, new { @class = "k-input" })</td>
<td><span class="error">@Html.ValidationMessageFor(u => u.ContactName)</span></td>
</tr>
<tr>
<td align="right">@Html.LabelFor(u => u.ContactPhone)</td>
<td align="left">@Html.TextBoxFor(u => u.ContactPhone, new { @class = "k-input" })</td>
<td align="left"><span class="error">@Html.ValidationMessageFor(u => u.ContactPhone)</span></td>
</tr>
<tr>
<td align="right">@Html.LabelFor(u => u.ContactEmail)</td>
<td align="left">@Html.TextBoxFor(u => u.ContactEmail, new { @class = "k-input" })</td>
<td align="left"><span class="error">@Html.ValidationMessageFor(u => u.ContactEmail)</span></td>
</tr>
<tr>
<td></td>
<td colspan="2"><input type="submit" value="Create" /></td>
</tr>
</table>
</fieldset>
</div>
}
任何帮助将不胜感激,总是进入保存模型,显然是给我一个空引用错误,当我提交一个空白表格。
答案 0 :(得分:0)
您遇到的至少一个问题是MVC会自动验证CompanyViewModel
(并且您在那里发布的数据构成有效的CompanyViewModel
),然后手动将其映射到Company
,这是无效的(我无法说明原因,因为你没有提供该类定义)。
您可以手动调用在回发中构建的Company
对象上的验证,或者您可以让MVC将发布的值绑定到Company
,这将导致它执行自动验证,如你期待。
如果你走这条路,你还需要编写一个方法来将Company
对象转换为CompanyViewModel
对象。
[HttpPost]
public ActionResult RegisterCompany(company.Company company)
{
if (ModelState.IsValid)
{
company.companycode = (new Guid()).ToString();
company.RegisterDate = DateTime.Now;
company.status = 3;
dc.companies.InsertOnSubmit(company);
dc.SubmitChanges();
}
else
{
var errors = ModelState.Values.SelectMany(v => v.Errors);
ModelState.AddModelError("", "Save Failed. Check the fields for errors");
}
var companyViewModel = GetViewModel(company); //transform left as an exercise to the author
return View(companyViewModel );
}