0
votes

Background

I have a bool in a view model class. I have an if statement checking to see if the bool is true or false. If false I set the value to true and call a view. In the view I have button that when clicked updates and calls the view again. My problem is that the bool keeps being set to false when the view is loaded. I don't know why this is happening. Is there a way to stop the bool from being changed?

Attempts

I'm aware MVC is stateless.

The view I've been given to work with is a .aspx view with the "value" attribute of the input tag already being used. So I'm unable to update the value of the Submit button.

<input type="submit" class="button" value="bvCalc" />

Question

In whatever way, I'm looking to persist the state of the bool. I don't know how to do this and I'm unsure as to whether or not the best thing to do at this point would be to

  1. use a hidden field in the view
  2. build an HTML Helper Extension that will update the bool from true to false
  3. return the modified value with the model information being sent back to the controller so I can evaluate the data in the if statement.
2
Can you not pass the bool back to your controller via the query string? Pass it in the routevalues of the form when it is submitted. It is in one of the overloads of the @Html.BeginForm() helper method. - yu_ominae
Thank you for the suggestion but management won't allow us to use a querystring on this project. Needs to be as single page app as possible, thanks again though. - Computer Guy
The you should specify FormMethod.Post, or use session variables for this. Post is probably better so you don't have to mess around with setting and erasing the variable each time. - yu_ominae
Just reread your comment... What does "as single-page app as possible mean"? If you post back to the same controller using a query string and then redirect to the same view, wouldn't that be a single-page app? If you do not want to post back, then you could use an ajax call to post back to the boolean value to your controller and the use a partial view to set your page depending on the controller's response. - yu_ominae
Sorry for the delay... I've been trying everything I can to make this work ...no luck so far... regarding managements "single-page-app" comment... just means that the user isn't allowed to see anything* in the url besides the domain name. - Computer Guy

2 Answers

1
votes

The way values get from a view to controller is via ModelBinding. If you are not familiar with how this works you might like to Google it to get a better understanding.

As a quick example, lets say the action method in your controller that you want to pass the bool to looks like this:

public ActionResult MyAction(MyViewModel model)
{
  //.. do something here.
}

and your ViewModel looks like this:

public class MyViewModel
{  
   public bool MyBool { get; set; }
   // ...more properties...
}

As long as your view has some kind of field (e.g. text box, checkbox, hidden field) with a name that exactly matches the property name (in this case 'MyBool') and a value that will convert to the property (in this example the string 'True' can map to to the boolean true value) - ModelBinding will be able to populate model.MyBool with true in your controller action.

If you use the built-in helper methods, you don't need to worry about getting the correct name/value in your field element - MVC will do this for you

e.g. @Html.HiddenFor(model => model.MyBool) will render HTML something like

<input type="hidden" id="MyBool" name="MyBool" value="True" />
0
votes

Use bool? In your model. Without it bool will always default to be false.