由于验证不能创建()/编辑()记录? [英] Cannot Create()/Edit() record due to Validation?
问题描述
我的库存跟踪程序有一个名为 INV_Assets
的主类。包括 INV_Locations
的 [ForeignKey]
几个子表的字段:
My Inventory Tracking program has a main class called INV_Assets
. This includes [ForeignKey]
fields for several child tables including INV_Locations
:
[Required]
public int Location_Id { get; set; }
[ForeignKey("Location_Id")]
public virtual INV_Locations Location { get; set; }
现在,在我的 Create()/ Edit() code>
用于View(此选择列表将我的的操作方法INV_Assets
我正在使用 ViewData []
元素> SelectList INV_Locations
字段中的2个组合到一个下拉列表中 - ex。[location_dept] | [location_room] - [IT] | [服务器]):
Now then, on my Create()/Edit()
action methods for INV_Assets
I am filling a ViewData[]
element with a SelectList
to be used on the View (this Select List combines 2 of my INV_Locations
fields into one within the dropdown - ex. [location_dept]|[location_room] - [IT]|[Server]):
ViewData["Location_Id"] = new SelectList((from l in db.INV_Locations.ToList() select new { location_room = l.location_dept + "|" + l.location_room }), "location_room", "location_room");
然后在我的视图中,我使用了 ViewData []
元素以填充 SelectList / DropDownListFor
:
Then on my View I used the ViewData[]
element to fill a SelectList/DropDownListFor
:
<span class="control-label col-md-2">Location:</span>
<div class="col-md-4">
@Html.DropDownListFor(model => model.Location_Id, (SelectList)ViewData["Location_List"], htmlAttributes: new { @class = "form-control dropdown", @id = "selectLocation" })
@Html.ValidationMessageFor(model => model.Location_Id, "", new { @class = "text-danger" })
</div>
我的DropDownList正如我打算正确填写,但在我做出选择之后(无论是更改选择 [IT] | [服务器] => [IT] | [Cubicle]
for Edit()或 [<< >>>>] => [IT] | [服务器]
为Create())一旦焦点移开,我得到验证/错误消息Field_Id必须是一个数字。
My DropDownList is correctly being filled as I intend, but after I make a Selection (be it changing the selection [IT]|[Server] => [IT]|[Cubicle]
for Edit() or [<<< SELECT >>>] => [IT]|[Server]
for Create()) once focus shifts away I get the Validation/Error message "The field Location_Id must be a number."
有更多经验的人可以重视我需要修改的内容,以保持此验证到位,但是适当实施?我可能会在DropDown视觉上组合字段[location_dept] | [location_room],但 INV_Location
的 Id
应该是一样的?
Can someone with more experience weigh in with what I need to modify to keep this validation in place, but to be properly implemented? I may be combining the fields [location_dept]|[location_room] visually in the DropDown, but the Id
for the INV_Location
should be the same?
如果下面有帮助,我的 INV_Locations
型号:
If it helps below is my INV_Locations
Model:
public class INV_Locations
{
public override string ToString()
{
return this.location_dept + "|" + this.location_room;
}
public int Id { get; set; }
[Display]
[Required(ErrorMessage = "Please enter a Location Dept.")]
public string location_dept { get; set; }
[Required(ErrorMessage = "Please enter a Room.")]
public string location_room { get; set; }
[Required]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime created_date { get; set; }
[Required]
public string created_by { get; set; }
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime? modified_date { get; set; }
public string modified_by { get; set; }
}
推荐答案
您的 Location_Id
与字符串的下拉列表
It appears you are population your dropdown for Location_Id
with strings
select new { location_room = l.location_dept + "|" + l.location_room }
您的模型希望它是一个int。
And your model expects it to be an int.
这篇关于由于验证不能创建()/编辑()记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!