我是ASP.NET的新手,我正在尝试修饰一个动作方法,允许管理员通过插入名字,姓氏和余额来创建新用户。操作方法应在checkingaccount表中生成Id,帐号和AppUserId。但是,后创建操作会抛出以下异常,因为我没有传递AppUserId
一个或多个实体的验证失败。看到 'EntityValidationErrors'属性以获取更多详细信息。
我可以在桌面上添加AppuserId吗?
创建操作方法
[HttpPost]
public ActionResult Create(string FName, string LName, string UserId, decimal balance)
{
var accountNumber = (12 + db.checkAccounts.Count()).ToString().PadLeft(10, '0');
var checking = new CheckingAccount
{
FirstName = FName,
LastName = LName,
AccountNumber = accountNumber,
Balance = balance,
AppUserId = UserId
};
db.checkAccounts.Add(checking);
db.SaveChanges();
return RedirectToAction("ViewAccounts");
}
CheckingAccount模型
public class CheckingAccount
{
public int Id { get; set; }
public string AccountNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Telephone { get; set; }
public decimal Balance { get; set; }
public virtual ApplicationUser User { get; set; }
[Display(Name = "User ID: ")]
[Required]
public string AppUserId { get; set; }
}
CheckingAccount模型
@model Project.Models.CheckingAccount
@{
ViewBag.Title = "Create";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Checking Account</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.AccountNumber)
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Balance, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Balance, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Balance, "", new { @class = "text-danger" })
</div>
</div>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.AppUserId)
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back", "ViewAccounts", null, new { @class = "btn btn-info" })
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
答案 0 :(得分:2)
您的ActionMethod参数与表单字段的名称完全匹配。例如,当您的实际模型属性为FName
和LName
时,您正在使用FirstName
和LastName
。因此,您的参数是空字符串。模型绑定器不知道FName
和FirstName
应该是相同的东西。
在操作方法中更正参数名称:
[HttpPost]
public ActionResult Create(string FirstName, string LastName, string AppUserId, decimal Balance){
或强>
由于您使用的是强类型视图,因此只需使用View中的模型作为Create ActionResult参数
[HttpPost]
public ActionResult Create(CheckingAccount model){
//fancy black magic binds all of the form properties
//to this model parameter for you!
model.AccountNumber = (12 + db.checkAccounts.Count()).ToString().PadLeft(10, '0');
db.checkAccounts.Add(model);
db.SaveChanges();
}
此外,您可以使用以下代码来获取特定的实体错误:
try{
db.SaveChanges();
}
catch (DbEntityValidationException e){
foreach (var error in e.EntityValidationErrors){
foreach (var propertyError in error.ValidationErrors){
Console.WriteLine($"{propertyError.PropertyName} had the following issue: {propertyError.ErrorMessage}");
}
}
}
答案 1 :(得分:1)
您可以使用try catch包装违规代码,并在catch中查看您需要解决的特定Entity Framework错误。如果您在此处发布详细错误后不知道该怎么做。但更多信息会有所帮助。
angular.module('angularModelerApp').directive('prettyprint', function($document) {
return {
restrict: 'C',
scope: {
xml: '@'
},
link: function postLink(scope, element, attrs) {
scope.$watch('xml', function(newVal, oldVal) {
element.text(vkbeautify.xml(newVal, 4));
});
}
};
});