Check Model for true values
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a model of mostly boolean properties with 5 additonal strings and an int. My question is there a way to check if there are at least 3 true values on this model. I know I can just loop through the values but my property names are currently different names instead of of checkbox1 checkbox2 and so on. I would like to keep the names unique if possible. I'm not sure if what I want is even possible.
Model:
namespace FacilitesPledgeForm.Models {
public class FacilitiesPledge
{
public bool LightsOff {get; set;}
public bool PowerDown {get; set;}
public bool PrintLess {get; set;}
public bool Stairs {get; set;}
public string test1 {get; set;}
public string test1 {get; set;}
public string test2 {get; set;}
public string test3 {get; set;}
public string test4 {get; set;}
public string test5 {get; set;}
public int num {get; set;}
}
}
Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Submit_PledgeForm(FacilitiesPledge facilitiesPledge)
{
//I want to loop here for at least 3 true values and if there isn't add an error to the model to make it invalid
if (!ModelState.IsValid)
{
return View("Index", facilitiesPledge);
}
facilitiesPledge.UserName = User.Identity.Name;
facilitiesPledge.Email = User.Identity.Name.ToUpper();
facilitiesPledge.Year = DateTime.Now.Year;
new PledgeFormStore().InsertPledgeAcceptance(facilitiesPledge);
return View("SuccessfulSubmission");
}
asp.net-mvc model
add a comment |
I have a model of mostly boolean properties with 5 additonal strings and an int. My question is there a way to check if there are at least 3 true values on this model. I know I can just loop through the values but my property names are currently different names instead of of checkbox1 checkbox2 and so on. I would like to keep the names unique if possible. I'm not sure if what I want is even possible.
Model:
namespace FacilitesPledgeForm.Models {
public class FacilitiesPledge
{
public bool LightsOff {get; set;}
public bool PowerDown {get; set;}
public bool PrintLess {get; set;}
public bool Stairs {get; set;}
public string test1 {get; set;}
public string test1 {get; set;}
public string test2 {get; set;}
public string test3 {get; set;}
public string test4 {get; set;}
public string test5 {get; set;}
public int num {get; set;}
}
}
Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Submit_PledgeForm(FacilitiesPledge facilitiesPledge)
{
//I want to loop here for at least 3 true values and if there isn't add an error to the model to make it invalid
if (!ModelState.IsValid)
{
return View("Index", facilitiesPledge);
}
facilitiesPledge.UserName = User.Identity.Name;
facilitiesPledge.Email = User.Identity.Name.ToUpper();
facilitiesPledge.Year = DateTime.Now.Year;
new PledgeFormStore().InsertPledgeAcceptance(facilitiesPledge);
return View("SuccessfulSubmission");
}
asp.net-mvc model
where is your relevant code?
– Ehsan Sajjad
Jan 4 at 16:06
I added sample code
– crmckain
Jan 4 at 16:22
add a comment |
I have a model of mostly boolean properties with 5 additonal strings and an int. My question is there a way to check if there are at least 3 true values on this model. I know I can just loop through the values but my property names are currently different names instead of of checkbox1 checkbox2 and so on. I would like to keep the names unique if possible. I'm not sure if what I want is even possible.
Model:
namespace FacilitesPledgeForm.Models {
public class FacilitiesPledge
{
public bool LightsOff {get; set;}
public bool PowerDown {get; set;}
public bool PrintLess {get; set;}
public bool Stairs {get; set;}
public string test1 {get; set;}
public string test1 {get; set;}
public string test2 {get; set;}
public string test3 {get; set;}
public string test4 {get; set;}
public string test5 {get; set;}
public int num {get; set;}
}
}
Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Submit_PledgeForm(FacilitiesPledge facilitiesPledge)
{
//I want to loop here for at least 3 true values and if there isn't add an error to the model to make it invalid
if (!ModelState.IsValid)
{
return View("Index", facilitiesPledge);
}
facilitiesPledge.UserName = User.Identity.Name;
facilitiesPledge.Email = User.Identity.Name.ToUpper();
facilitiesPledge.Year = DateTime.Now.Year;
new PledgeFormStore().InsertPledgeAcceptance(facilitiesPledge);
return View("SuccessfulSubmission");
}
asp.net-mvc model
I have a model of mostly boolean properties with 5 additonal strings and an int. My question is there a way to check if there are at least 3 true values on this model. I know I can just loop through the values but my property names are currently different names instead of of checkbox1 checkbox2 and so on. I would like to keep the names unique if possible. I'm not sure if what I want is even possible.
Model:
namespace FacilitesPledgeForm.Models {
public class FacilitiesPledge
{
public bool LightsOff {get; set;}
public bool PowerDown {get; set;}
public bool PrintLess {get; set;}
public bool Stairs {get; set;}
public string test1 {get; set;}
public string test1 {get; set;}
public string test2 {get; set;}
public string test3 {get; set;}
public string test4 {get; set;}
public string test5 {get; set;}
public int num {get; set;}
}
}
Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Submit_PledgeForm(FacilitiesPledge facilitiesPledge)
{
//I want to loop here for at least 3 true values and if there isn't add an error to the model to make it invalid
if (!ModelState.IsValid)
{
return View("Index", facilitiesPledge);
}
facilitiesPledge.UserName = User.Identity.Name;
facilitiesPledge.Email = User.Identity.Name.ToUpper();
facilitiesPledge.Year = DateTime.Now.Year;
new PledgeFormStore().InsertPledgeAcceptance(facilitiesPledge);
return View("SuccessfulSubmission");
}
asp.net-mvc model
asp.net-mvc model
edited Jan 4 at 16:21
crmckain
asked Jan 4 at 16:05
crmckaincrmckain
1057
1057
where is your relevant code?
– Ehsan Sajjad
Jan 4 at 16:06
I added sample code
– crmckain
Jan 4 at 16:22
add a comment |
where is your relevant code?
– Ehsan Sajjad
Jan 4 at 16:06
I added sample code
– crmckain
Jan 4 at 16:22
where is your relevant code?
– Ehsan Sajjad
Jan 4 at 16:06
where is your relevant code?
– Ehsan Sajjad
Jan 4 at 16:06
I added sample code
– crmckain
Jan 4 at 16:22
I added sample code
– crmckain
Jan 4 at 16:22
add a comment |
1 Answer
1
active
oldest
votes
add your Boolean members to list
using linq check if count of true Boolean member more than or equeal to 3 reteurn true
public class FacilitiesPledge
{
public bool LightsOff { get; set; }
public bool PowerDown { get; set; }
public bool PrintLess { get; set; }
public bool Stairs { get; set; }
public string test1 { get; set; }
public string test2 { get; set; }
public string test3 { get; set; }
public string test4 { get; set; }
public string test5 { get; set; }
public int num { get; set; }
public bool IsValid()
{
//add your boolean members
List<bool> items = new List<bool> { LightsOff, PowerDown, PrintLess, Stairs };
//if count of true Boolean member more than or equeal to 3 reteurn true
bool isValid = items.Count(x => x == true) >= 3;
return isValid;
}
}
thank you so much.
– crmckain
Jan 17 at 14:46
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54042405%2fcheck-model-for-true-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
add your Boolean members to list
using linq check if count of true Boolean member more than or equeal to 3 reteurn true
public class FacilitiesPledge
{
public bool LightsOff { get; set; }
public bool PowerDown { get; set; }
public bool PrintLess { get; set; }
public bool Stairs { get; set; }
public string test1 { get; set; }
public string test2 { get; set; }
public string test3 { get; set; }
public string test4 { get; set; }
public string test5 { get; set; }
public int num { get; set; }
public bool IsValid()
{
//add your boolean members
List<bool> items = new List<bool> { LightsOff, PowerDown, PrintLess, Stairs };
//if count of true Boolean member more than or equeal to 3 reteurn true
bool isValid = items.Count(x => x == true) >= 3;
return isValid;
}
}
thank you so much.
– crmckain
Jan 17 at 14:46
add a comment |
add your Boolean members to list
using linq check if count of true Boolean member more than or equeal to 3 reteurn true
public class FacilitiesPledge
{
public bool LightsOff { get; set; }
public bool PowerDown { get; set; }
public bool PrintLess { get; set; }
public bool Stairs { get; set; }
public string test1 { get; set; }
public string test2 { get; set; }
public string test3 { get; set; }
public string test4 { get; set; }
public string test5 { get; set; }
public int num { get; set; }
public bool IsValid()
{
//add your boolean members
List<bool> items = new List<bool> { LightsOff, PowerDown, PrintLess, Stairs };
//if count of true Boolean member more than or equeal to 3 reteurn true
bool isValid = items.Count(x => x == true) >= 3;
return isValid;
}
}
thank you so much.
– crmckain
Jan 17 at 14:46
add a comment |
add your Boolean members to list
using linq check if count of true Boolean member more than or equeal to 3 reteurn true
public class FacilitiesPledge
{
public bool LightsOff { get; set; }
public bool PowerDown { get; set; }
public bool PrintLess { get; set; }
public bool Stairs { get; set; }
public string test1 { get; set; }
public string test2 { get; set; }
public string test3 { get; set; }
public string test4 { get; set; }
public string test5 { get; set; }
public int num { get; set; }
public bool IsValid()
{
//add your boolean members
List<bool> items = new List<bool> { LightsOff, PowerDown, PrintLess, Stairs };
//if count of true Boolean member more than or equeal to 3 reteurn true
bool isValid = items.Count(x => x == true) >= 3;
return isValid;
}
}
add your Boolean members to list
using linq check if count of true Boolean member more than or equeal to 3 reteurn true
public class FacilitiesPledge
{
public bool LightsOff { get; set; }
public bool PowerDown { get; set; }
public bool PrintLess { get; set; }
public bool Stairs { get; set; }
public string test1 { get; set; }
public string test2 { get; set; }
public string test3 { get; set; }
public string test4 { get; set; }
public string test5 { get; set; }
public int num { get; set; }
public bool IsValid()
{
//add your boolean members
List<bool> items = new List<bool> { LightsOff, PowerDown, PrintLess, Stairs };
//if count of true Boolean member more than or equeal to 3 reteurn true
bool isValid = items.Count(x => x == true) >= 3;
return isValid;
}
}
answered Jan 4 at 19:20
Ahmed GhoniemAhmed Ghoniem
545510
545510
thank you so much.
– crmckain
Jan 17 at 14:46
add a comment |
thank you so much.
– crmckain
Jan 17 at 14:46
thank you so much.
– crmckain
Jan 17 at 14:46
thank you so much.
– crmckain
Jan 17 at 14:46
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54042405%2fcheck-model-for-true-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
where is your relevant code?
– Ehsan Sajjad
Jan 4 at 16:06
I added sample code
– crmckain
Jan 4 at 16:22