0

So basically I have a bool in my model, and in my view I have a hidden field that I set to either true or false when I have a certain select box selected. Now that I have this I need to somehow check the value of the hidden field and the value of the bool in the controller. If it is true I want to run a method and if it is false I want it to skip the method.

Here is my html the invisible field is at the bottom:

<div id="addTransfereeForm" class= "invisible" "whiteborder">
    <form name="addTransferee" id="addTransferee">
    <p><b><u>Add a New Transferee</u></b></p>
        Transfer Effective Date: <input type="text" name="transfereffectivedate"     class="datepicker"<br /> <br />
        Transferee Name: <input type="text" name="transfereename" /><br />
        Transferee Notice Address Line 1: <input type="text" name="transfereenoticeaddressline1" /><br />
        Transferee Notice Address Line 2: <input type="text" name="transfereenoticeaddressline2" /><br />
        Transferee Notice City: <input type="text" name="transfereenoticecity" /><br />
        Transferee Notice State: <input type="text" name="transfernoticestate" /><br />
        Transferee Notice Zip: <input type="text" name="tranfernoticezup" /><br />
        Transferee Notice Phone: <input type="text" name="transfereenoticephone" /><br />
        Transferee Payment Address Line 1: <input type="text" name="transfereepaymentaddressline1" /><br />
        Transferee Payment Address Line 2: <input type="text" name="transfereepaymentaddressline2" /><br />
        Transferee Payment City: <input type="text" name="transfereepaymentcity" /><br />
        Transferee Payment State: <input type="text" name="transfereepaymentstate" /><br />
        Transferee Payment Zip: <input type="text" name="transfereepaymentzip" /><br />
        Transferee Payment Phone: <input type="text" name="transfereepaymentphone" /><br />
        <input type="hidden" name="newtransferee" />
    </form>
</div>

Here is my Model(NewTransferee is the bool I'm setting to true or false):

 public class CreateTransferee
{
    public DateTime? TransferEffectiveDate { get; set; }
    public string TransfereeName { get; set; }
    public string TransfereeNoticeAddressLine1 { get; set; }
    public string TransfereeNoticeAddressLine2 { get; set; }
    public string TransfereeNoticeCity { get; set; }
    public string TransfereeNoticeState { get; set; }
    public string TransfereeNoticeZip { get; set; }
    public string TransfereeNoticePhone { get; set; }
    public string TransfereePaymentAddressLine1 { get; set; }
    public string TransfereePaymentAddressLine2 { get; set; }
    public string TransfereePaymentCity { get; set; }
    public string TransfereePaymentState { get; set; }
    public string TransfereePaymentZip { get; set; }
    public string TransfereePaymentPhone { get; set; }
    public bool NewTransferee { get; set; }
}

Here is my controller (I need to edit this to make it check for the bool value before running the end portion):

 [NoCache]
    [HttpPost]
    [ValidateAntiForgeryToken]

    public JsonResult ResolveProfileSelectionRequired(ResolveProfileSelectionRequiredModel model)
    {
        var resolved = SqlDataProvider.ResolveProfileSelectionRequiredJobException(model);

        return resolved ? Json("Success") : Json("Failed");
    }

Finally I have the method I created to run if the bool is true(this is located in a SQLDataProvider.cs):

public static int CreateNewTransferee(CreateTransferee model)
    {

        using (var db = new NJB.NJBDataContext())
        {
            var result = db.SP_AddTransferee(
            model.TransferEffectiveDate,
            model.TransfereeName,
            model.TransfereeNoticeAddressLine1,
            model.TransfereeNoticeAddressLine2,
            model.TransfereeNoticeCity,
            model.TransfereeNoticeState,
            model.TransfereeNoticeZip,
            model.TransfereeNoticePhone,
            model.TransfereePaymentAddressLine1,
            model.TransfereePaymentAddressLine2,
            model.TransfereePaymentCity,
            model.TransfereePaymentState,
            model.TransfereePaymentZip,
            model.TransfereePaymentPhone);

           return (int) result.ReturnValue;
        }
    }
3
  • And... What is the question? Does model.NewTransferee not contain the expected value in the controller action?
    – David
    Sep 24, 2013 at 14:09
  • 1
    Use <input type="hidden" ... />` for invisible input fields.
    – Raidri
    Sep 24, 2013 at 14:13
  • @David I'm a little new to MVC and I am looking to simply check the value of the bool in the controller which I am having trouble even starting at all. I need to edit that portion of the controller to check whether the bool value is true or false when the page POSTs. Then run the method based on that check.
    – Nick_M
    Sep 24, 2013 at 14:13

3 Answers 3

1

If all you need to do is check the value of a boolean, that's just a conditional statement:

public JsonResult ResolveProfileSelectionRequired(ResolveProfileSelectionRequiredModel model)
{
    if (model.NewTransferee)
    {
        // do something
    }
    else
    {
        // do something else
    }
}

It's not entirely clear what your goal is though, since your current return statement depends on the output of the method that you only want to call conditionally. All code paths will need to return something. Maybe like this?:

public JsonResult ResolveProfileSelectionRequired(ResolveProfileSelectionRequiredModel model)
{
    if (model.NewTransferee)
    {
        var resolved = SqlDataProvider.ResolveProfileSelectionRequiredJobException(model);

        return resolved ? Json("Success") : Json("Failed");
    }
    else
    {
        return Json("Success");
    }
}

Again, I can't know without knowing more about your intended behavior for this action.

It's worth noting that this isn't really an "MVC" thing. At its simplest, this action method is just a method which receives an argument of type ResolveProfileSelectionRequiredModel. The conditional statement is just plain C#. All code paths returning a value is plain C#. The only thing "MVC" about this method is that it's returning a JsonResult.


Edit: I just noticed... The value you're looking for is on the CreateTransferee model, but your controller is receiving a ResolveProfileSelectionRequiredModel? Is that value on the ResolveProfileSelectionRequiredModel? They seem to be very similar. If it's not on that model, you can either add it to that model or use @Raidri's approach to add it as an explicit parameter to the controller action.

1

Expand your controller action with a new parameter:

public JsonResult ResolveProfileSelectionRequired(ResolveProfileSelectionRequiredModel model, bool newtransferee = false)

and check it inside the action.

2
  • Ok, I know that this is a noob question, but is the best way to check this through an if-else statement and apply the method that needs to be run when true and pass on the method when false?
    – Nick_M
    Sep 24, 2013 at 14:36
  • 1
    Yes (an if statement should be enough, no need for the else)
    – Raidri
    Sep 24, 2013 at 14:38
0

You could use:

if (Request.Form["newtransferee"].ToLower() == "true")

or does it work if you write this instead of the manual html?

@Html.HiddenFor(model => model.NewTransferee)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.