Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
 [Required]
public int DepartmentID { get; set; }

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
C#
 // [Required]
public int? DepartmentID { get; set; }

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

C#
[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?)


What I have tried:

I tried to change int to int? and removing Department Id from (ModelState.IsValid)
Posted
Updated 29-Nov-17 6:47am
v3

1 solution

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

C#
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=""})


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.
 
Share this answer
 
Comments
Member 10754595 29-Nov-17 13:32pm    
sorry did not work
got an error below.

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Documents_dbo.Departments_DepartmentID". The conflict occurred in database "ABC", table "dbo.Departments", column 'ID'.
The statement has been terminated.
David_Wimbley 29-Nov-17 13:42pm    
I would say what I described did work as your now getting an error attempting to insert data into your database which is separate from what you first asked about.

I don't know anything about your table structure nor do i have access to your code but it seems to me the error is pretty clear. You've got a foreign key constraint that requires a DepartmentId to be selected.

Either add an option in your departments table of "NOT SELECTED" which would provide you a valid ID to insert into your documents table or leave Department as required. If you still can't leave it as required and you don't want to insert an option into the departments table as "NOT SELECTED" then you need to re-evaluate your table structure because your not going to be able to accomplish making DepartmentId optional with a foreign key in your DB making it required. The left hand has to talk to the right hand here.
Member 10754595 29-Nov-17 14:06pm    
Now it did work (DepartmentID ?? 1) with 1 not with 0
Thanks a lot for your help! it saves my day.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900