如何从MVC5中提交的主键中删除ID所需的验证 [英] How to remove ID required validation from primary key filed in MVC5

查看:62
本文介绍了如何从MVC5中提交的主键中删除ID所需的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究MVC5中的一个现有项目,因为在我的创建模型中有一个名为Department的下拉列表,我在下面要求提交。

I am working on an existing project in MVC5 in that there is a drop-down called Department in my create model I have below required filed.

 [Required]
public int DepartmentID { get; set; }



现在使用它的人不希望进行必要的验证我试图通过在int之后添加(?)来使其可为空。但它不是让我改变,我在我的控制器上得到一个错误不能隐式地将类型'int?'转换为'int'。存在显式转换(您是否错过了演员?)我认为因为主键但不确定任何人都可以帮助我,请


Now the person who is using this does not want required validation I tried to make it nullable by adding(?) after int. but it is not letting me change, I am getting an error on my controller Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?) I think because of primary key but not sure can anyone help me, please

 // [Required]
public int? DepartmentID { get; set; }



我也通过在我的控制器中添加来尝试这一行

ModelState.Remove(DepartmentID);




and also I tried this line by adding in my controller
ModelState.Remove("DepartmentID");

[ValidateAntiForgeryToken]
       public ActionResult Create(CreateModel model)
       {
           ModelState.Remove("DepartmentID");
           if (ModelState.IsValid)
           {

               document.DepartmentID = model.DepartmentID; // Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)





我尝试了什么:



我试图将int更改为int?从(ModelState.IsValid)



What I have tried:

I tried to change int to int? and removing Department Id from (ModelState.IsValid)

推荐答案

删除部门ID我不认为这与主键有任何关系,除非你说DepartmentId是主键。



我的建议是将DropDownListFor数据添加到Select的默认选项,并将Select的值设置为0.因为你的字段是一个整数并且假设它在数据库中的要求比它原来要为0。如果0是可能的值,则将值设置为...- 1.



所以你的代码看起来像



I don't think this has anything to do with primary key, unless you are saying DepartmentId is a primary key.

My suggestion would be to add to your DropDownListFor data a default option of "Select" and set the value of that Select to 0. Since your field is an integer, and assuming its required as such in the database than its going to be 0 anyway. If 0 is a possible value, then set the value to say...-1.

So your code would look like

var selectData = new List<SelectListItem>();
selectData.Add(new SelectListItem { Text ="Select", Value = "-1"});
selectData.Add(new SelectListItem { Text ="Actual Selection", Value = "Actual Value"});

@Html.DropDownListFor(m=>m.DepartmentID, selectData, new{@class=""})





这样,如果没有做出选择,你知道-1表示他们没有选择任何值。



另一种选择是将其作为-1传递并且当验证运行时,如果模型中的内容为null,则使用null coalescing运算符将值默认为0.



Ex: var safeDeptId =( DepartmentID ?? 0); 这样,如果没有选择任何选项,你的默认值为0,不应该得到错误,表明你错过了一个演员。



关于如何解决问题的一些想法。



This way, if no selection is made, you know that -1 means they didnt choose any values.

The other option is to pass it in as -1 and when the validation runs, use the null coalescing operator to default the value to 0 if what comes through from the model is null.

Ex: var safeDeptId = (DepartmentID ?? 0); That way if no option was selected, your defaulting to 0 and shouldn't get the error indicating you are missing a cast.

Just some ideas on how to solve your problem.


这篇关于如何从MVC5中提交的主键中删除ID所需的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆