last day (16 days later) » 

4:38 PM
0
Q: MVC 5 - Default radio button to nothing selected

CB4I have a "div" with id="SendMeCatalog" which only displays on the page when the user selects to buy a product that has Catalog available. Hidding/showing the div is controlled by JavaScript. The reason is because some product doesn't have Catalog available and if the user selects a product that d...

 
do you use client side validation?
 
CB4
for client side, I only have @Html.ValidationMessageFor(model => model.SendThemCatalog, "Please select Yes/No")
 
I guess you dont use client side validation then you can use ModelState.Remove method by checking if product has no catolog available with your custom method
 
CB4
can you please tell me more about why you think I should use the ModelState.Remove method in this case? I've tested it and it works; but I would like to understand why I should use it and when should I not be using it. Thanks
 
You can use this method for special conditions to omit validaiton for one of your model field and in this case if your models has relation with eachother you can change condition part like checking product's catalog count
Dont use it this Method for strongly specified validations. But in this case your fied is nullable so you can use this :)
 
CB4
4:38 PM
so which method should i use now?
 
hi
 
CB4
4:56 PM
hi
sorry, so which method should i use?
 
do your models have a relation ?
like primary- foreign key
It seems you have two option before perform ModelState.Remove
 
CB4
yes i do
my db has a lot of tables
and they are related
 
so you can check model product's catolog count in if condition before implement ModelState.Remove
 
CB4
no I can't easily do that
i have many models, one models is for the Products, and one model is for the Customer
 
then you can create HasCatolog custom method to understand whether product has catolog or not
I guess You have tried second one
 
CB4
5:05 PM
i can't try second one
i am not exaclty sure where that method should go
 
second one is creating HasCatolog custom method to understand whether product has catolog or not
 
CB4
do i place that method in my controller?
 
yes you can place it in your controller
You said I tried and it works , What did you try exactly
 
CB4
i tried to put that method in my controller, but then in my model it gives me error when i added this line [CustomValidation(typeof(YourModel), nameof(ValidateSendThemCatalog))]
 
Did you try my answer?
 
CB4
5:08 PM
it can't find the ValidateSendThemCatalog
i did try your answer
 
with conditino?
 
CB4
your answer passed when i only choose one product
 
condition?
 
CB4
but if i pick multiple products, and if at least one of those products doesn't have Catalog, then your code failed
let me rephrase that,
but if i pick multiple products, and if some products has Catalog and some doesn't have catalogs, then your code failed
 
even if at one least product dont have catalog which has a catolog option, Your model should not be valid
you are checking posibility of that whether product has a catolog option and dont have selected catolog option at the same
 
CB4
5:16 PM
my application only displays the <div> where id="SendMeCatalog" if all of the product selected has Catalog. So as long as one of the selected product DOESN'T have Catalog then this <div> will be hidden\
 
Can you share your models in question
I can add new condition with entity linq to detect that even if at least one product dont have selected catolog and have selected option at the same time
 
CB4
public class ProductViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
public bool? AllowCatalog { get; set; }
}
public class StartBuyingViewModel
{
[Required]
public bool? SendThemCatalog { get; set; }
/*
* lots more here
*/
}
the page where the client picks the product is utilizing StartBuyingViewModel
public class ProductViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
public bool AllowCatalog { get; set; }
}
there was a typeo in the first ProductViewModel -> it should be

public bool AllowCatalog { get; set; }
 
5:31 PM
propably you are getting list from view in controller
 
CB4
yes
 
I have to go for half an hour and i will write condition method
 
CB4
thank you very much for your help
 
your welcome
as a last
where is yout catolog model
it is also important
 
CB4
Catalog doesn't have a model
its simply a Yes/No radio button
if user select yes, then we will send them email seperately
 
5:38 PM
what was the option hidden condition
 
CB4
so let me recap

we have a product model, and it has a field AllowCatalog

then we have a StartBuying model, and it will look at all the product selected by client and determine if the SendThemCatalog radio button appears or not. Like I said, as long as one product selected that doesn't have catalog then this radio button will be hidden and it should set to False
 
Ok I see i will be back in half an hour
 
CB4
Thank you very very much for your kindness on helping
 
6:23 PM
cb4?
so you can make SendThemCatalog default false right?
 
CB4
i can make it default false
but i don't want it to be showing false on the Radio button
I want it to appears Not Selected Any first and then the client will click on Yes or No radio button
 
yes i see so we can assign false to null values in controller right?
after view?
 
CB4
yes
 
can you add StartBuyingViewModel totally
 
CB4
i want the value to set to false whenever there is one product in the selected list does not have catalog
i am sorry, what do you mean by "
can you add StartBuyingViewModel totally"
 
6:37 PM
you just added one property of that
does it have List<ProductViewModel>
 
CB4
it has public List<ProductCategorySelection> Categories { get; set; } and a product does belong to a catagory
 
can you add this code your controller if it works I will update my answer
if( model.SendThemCatalog == null){
model.SendThemCatolog = false;
ModelState.Remove("SendThemCatolog")

}
 
CB4
i got a modelstate error
when i select multiple products with one product that doesn't have catalog
 
can you add controller here
 
CB4
in the httppost method, it hit the if statement and it change the status to false and then remove it, but it failed
 
6:48 PM
situation is so strange
can you add all related object here with controller
 
CB4
the code is massive
i only show what is relevant
when i do this

foreach (ModelState state in ViewData.ModelState.Values.Where(x => x.Errors.Count > 0))
{
var a = state.Errors;
}
i see the error but the key is ""
before the key used to say "SendThemCatalog"
 
What about allowcatolog field
do you have hangout account
there is logic error i guess
 
CB4
the allowcatalog states has no error
 
because it is not required
 
CB4
thats right
so where could the problem be
 
6:59 PM
startbuying model is your main model right
 
CB4
it is the model where the controller is validating the modelstates
 
public class StartBuyingViewModel
{
[Required]
public bool? SendThemCatalog { get; set; }
public List<ProductViewModel > products { get; set; }
}
is it like that
 
CB4
it doesn't have public List<ProductViewModel > products { get; set; }
 
may i want you to add all properties of this model
 
CB4
it has public List<ProductCategorySelection> Categories { get; set; }
and any product will belong to some catagory
 
7:04 PM
where is your ProductCategorySelection
 
CB4
public ProductCategorySelection()
{
this.Products = new List<ProductSelection>();
}

public string Category { get; set; }
public string Category_FR { get; set; }
public List<ProductSelection> Products { get; set; }
 
ProductSelection?
there is huge chain
 
CB4
yes that is what i meant. it is massive
but the relevant code is what i trying to show you
ProductSelection has this


public Product Product { get; set; }
Product is ProductViewModel
 
can you remove [Required] attribute
to test
 
CB4
but then how can i validate that ?
 
7:11 PM
just for test
 
CB4
ah ok let me test
 
to understand whether problem is there or not
 
CB4
if i remove the Required then it works
 
also for multiple products?
 
CB4
yes
 
7:17 PM
okay i guess i got the point
model can be multiple right
and then we can not perform model.SendThemCatalog == false
we need multiple solution
 
CB4
it sounds right
 
can you try this
if(model.where(x=>x.SendThemCatalog == null).Count() > 0){


}
 
CB4
just that?
 
np
no
in if condition
model = model.Where(x => x.SendThemCatalog == null).ToList().ForEach(x => x.SendThemCatalog = false);
can you try
at least i got the point :)
we are so close
 
CB4
i got intellisene error
.where and .ToList
it said my model does not contain a difinition for Where..... and ToList
 
7:32 PM
can you write foreach for model like
foreach(var item in model){
item.SendThemCatalog = (item.SendThemCatalog == null) ? false : item.SendThemCatalog;
}
can you add this foreach into your if condition
if(model.where(x=>x.SendThemCatalog == null).Count() > 0){
foreach(var item in model){
item.SendThemCatalog = (item.SendThemCatalog == null) ? false : item.SendThemCatalog;
}

}
 
CB4
2 compiling errors.

1- it says my model does not contain a difinition for Where
2 - it says my model does not contain a public difinition for GetEnumerator
 
just write it to test
foreach(var item in model){
item.SendThemCatalog = (item.SendThemCatalog == null) ? false : item.SendThemCatalog;
}
 
CB4
compiling errors.
it says my model does not contain a public difinition for GetEnumerator
 
you said it can be multiple
can you add using System.Linq;
 
CB4
i already have it
 
7:43 PM
you should convert your model IEnumerable<model>
 
CB4
but i looks like its only one model
 
no it can be multiple
yes one model
but it can be multiple
so you need to use IEnumerable usages
 
CB4
so i added this

@model IEnumerable<DucaApplication.Models.StartBuyingViewModel> to my .cshtml
 
good
can you retry
 
CB4
but foreach (var item in model) still gives error on model
 
7:48 PM
?
There is a huge logic error
i guess it is very hard to solve problem without see all codes
would you like to use hangout or something like that
 
CB4
this whole problem started when i wanted to added that [Required] to the field
and add ? to the property to make it null by default
i can't believe such a small change can cause a big problem
 
this approach is not wrong but there is also ienumarable problem
you are not able to use list methods for this model now
can you try
foreach(var item in model.AsEnumerable() ){
item.SendThemCatalog = (item.SendThemCatalog == null) ? false : item.SendThemCatalog;
}
with
@model IEnumerable<DucaApplication.Models.StartBuyingViewModel> to your .cshtml
 
CB4
8:04 PM
btw the foreach loop goes into the controller right?
 
yes
 
CB4
i got compling error no Extension method for AsEnumerable
 
can you make model.ToList()
 
CB4
nope
i can do model.Categories
i can do something like this

foreach (var item in model.Categories)
{
foreach (var p in item.Products)
{

}
}
 
but you said model can be multiple
 
CB4
8:10 PM
i am sorry that was my fault
 
CB4
8:30 PM
do you have any suggestion?
 
so your model length always 1 right?
 
CB4
like how i even tell that?
 
i have to go for 40 minutes
i will be back
can you add your controller code with get and post methods to understand the problem
 
CB4
9:03 PM
i think that would make you more confuse
like there is a big chain
i am going to be away for a while. can we chat again tomorrow?
again, i am really thank you for your help today
it was very kind of you
 
9:43 PM
yes we can
sure:)
your welcome:)
 

  last day (16 days later) »