c# - System.NotSupportedException ASP.net MVC
问题描述
我正在尝试验证控制器中的数据。我传递的数据是从同一视图中的另一个字段复制的。满足数据条件,数据正在传递,但是数据传递后,我遇到了错误 System.NotSupportedException ,我也无法跟踪。
查看 [我隐藏了几个字段并将数据从另一个字段复制到这些字段中,因为我无法使用 SSN 验证 TaxID 数据]:
<body>
@Html.DropDownListFor(x => x.TAxIDType, Model.TAxIDType, new { id = "taxid" })
@Html.ValidationMessageFor(x => x.TAxIDType)
@Html.TextBoxFor(x => x.TaxID, new { id="taxidinput", @class = "feintextbox", maxlength = 9, @placeholder = "xxxxxxxxx" })
@Html.ValidationMessageFor(x => x.TaxID)
@Html.TextBoxFor(x => x.FEIN, new { id="feinfield", @class = "feintextbox", maxlength = 10, @placeholder = "xxxxxxxxx" })
@Html.ValidationMessageFor(x => x.FEIN)
@Html.TextBoxFor(x => x.SSN, new { id="ssnfield", @class = "ssntextbox", maxlength = 10, @placeholder = "xxxxxxxxx" })
@Html.ValidationMessageFor(x => x.SSN)
</body>
<script>
$(function () {
$('#feinfield').hide();
});
$(function () {
$('#ssnfield').hide();
});
$("#taxidinput").keypress(function () {
$("#ssnfield").val($(this).val());
});
$("#taxidinput").keypress(function () {
$("#feinfield").val($(this).val());
});
</script>
控制器 [我只显示一个条件(SSN 验证)]:
[HttpPost]
public ActionResult CorporationRegistrationPg1(Testing CorporationRegistration, string EmailID, int SSN, int FEIN, string TAxIDType)
{
if (ModelState.IsValid)
{
using (SUPRTestingDBEntities2 db = new SUPRTestingDBEntities2())
{
if (TAxIDType == "SSN")
{
var obj = db.SUPRTesting.Where(a => a.EmailID.Equals(CorporationRegistration.EmailID) && a.SSN.Equals(CorporationRegistration.SSN)).FirstOrDefault();
//This is where I am getting the error
//System.NotSupportedException
//The specified type member 'FEIN' is not supported in LINQ to Entities.
//Only initializers, entity members, and entity navigation properties are supported.
if (obj != null)
{
if (obj.Active == 0 && obj.Submit == 0)
{
Session["LoginID"] = obj.LoginID.ToString();
Session["EmailAddress"] = obj.EmailID.ToString();
return RedirectToAction("CorporationRegistrationPg2");
} else
{
var Testing = new Testing();
return View(Testing);
}}
else if (obj == null)
{
return View();
}
}
我已经声明了控制器中的所有字段。然而,我的控制器出现错误。
模型:
public partial class Testing
{
public int LoginID { get; set; }
[Required]
public string EmailID { get; set; }
public int TaxID { get; set; }
public int SSN { get; set; }
public int FEIN { get; set; }
public int Active { get; set; }
public int Submit { get; set; }
public List<SelectListItem> TAxIDType = new List<SelectListItem>()
{
new SelectListItem() {Text="Select here", Value="default"},
new SelectListItem() {Text="SSN", Value="SSN"},
new SelectListItem() { Text="FEIN", Value="FEIN"}
};
}
有人可以帮我弄这个吗。
解决方案
我假设常量/枚举CorporationRegistration.EmailID
并在通过实体框架检索数据时CorporationRegistration.SSN
导致异常。NotSupported
尝试将它们更改为整数或字符串(取决于它们的基础数据类型),例如:
int corpRegistrationEmailId = (int) CorporationRegistration.EmailID;
int corpRegistrationSSN = (int) CorporationRegistration.SSN;
或字符串:
string corpRegistrationEmailId = CorporationRegistration.EmailID.ToString();
string corpRegistrationSSN = CorporationRegistration.SSN.ToString();
然后直接在.Where
子句中引用变量。
var obj = db.SUPRTesting
.Where(a => a.EmailID.Equals(corpRegistrationEmailId) &&
a.SSN.Equals(corpRegistrationSSN)).FirstOrDefault();
从您的更新(包括测试模型)。您可能想要删除List<SelectListItem>
TaxIDType 的属性,实体框架在将此类型转换为基础 SQL 类型时可能会遇到问题,因此出现 NotSupported 异常。
作为最佳实践,您可能希望重构 Controller 并将数据库逻辑移动到适当的层(例如 DAL / 服务层),而不是直接在 Controller 级别执行数据库操作。
作为附加说明,您还可以专注于缩进/代码样式并从代码库中删除魔术字符串/魔术数字,这将使您的代码更易于阅读并有助于可维护性。
推荐阅读
- conv-neural-network - 许多标签的对象检测,如何做到这一点?
- html - 如何在 CSS 中覆盖背景颜色?
- amazon-web-services - 您如何使用适用于 Go 的 AWS X-Ray 开发工具包跟踪微服务?
- javascript - 如何使用 python 操作 html(或 javascript)来抓取数据条目
- python - AttributeError:“NoneType”对象没有属性“get_text”。BeautifulSoup 对象失败
- arrays - 如何找到最简洁的升序子数组
- mongodb - 查找区域内的地理点
- c++ - 将指向列表中元素的指针插入映射会导致垃圾值
- python - python浮点运算的不同答案
- firebase - 有没有办法在 Firestore 中查询为空或包含特定值的数组的字段?