0

I am doing some server stuff that could potentially fail (not likely) but something can go wrong. I want to stop the user from going any further so I want to disable the input so that they have to refresh the page. How can I disable it based on ModelState errors. If any errors exist, disable this box and I only want to do it when the page is initially loaded. My problem is not necessarily how to do it but more what is the standard and the best way to do it. I could have a ViewBag that holds a true false for disabling it but not sure if that's the best route, any suggestions?

Pittfall
  • 2,751
  • 6
  • 32
  • 61
  • What kind of server stuff could fail? Since you're detecting errors server side, you are already sending the client a new page when you return `View(model)`. – jrummell Jun 12 '12 at 19:04
  • what do you mean by "Refresh the page"? its up to you what they will receive/see when you set the view on failure. If you prefer to your method - you can simply access the ModelState from your view and render an input with a disabled attribute on error – YavgenyP Jun 12 '12 at 19:08
  • I populate a combobox from the database, let's say the connection fails for some reason. That kind of fail which is why if they refresh, everything could be ok – Pittfall Jun 12 '12 at 19:41
  • If you get error on service call, you may wrap your service calls in try/catch block, and send warning/information message to the View. – Yusubov Jun 13 '12 at 12:25

2 Answers2

3

Let's suppose that you have a POST controller action that does some processing and adds errors to the ModelState if it fails which pretty standard. Now inside the view you could:

@Html.TextBoxFor(
    x => x.SomeProperty, 
    !ViewData.ModelState.IsValid ? new { @readonly = "readonly" } : null
)

or write a custom helper that will do the job and simplify things up for you:

@Html.MyTextBoxFor(x => x.SomeProperty)

Could be useful if you need this behavior for multiple input fields.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Unless I am missing something, this will not work. I can't always disable the text box on ModelState.IsValid. If the connection fails right at the beginning of the load, then it needs to be disable but to add a little bit more detail, this is a registration form and if the user submits and they have a duplicate user name, I will set a modelState error but I don't want the text box to be disabled or readOnly in that case – Pittfall Jun 13 '12 at 13:28
  • Sorry I don't understand your scenario. – Darin Dimitrov Jun 13 '12 at 13:33
  • So basically, I need the textbox disabled if a server error occurs on page load using the ModelState but if the form is submitted and there are ModelState errors, I do not want to disable the textbox because it might not be a server error when the form is submitted, it could just be a validation condition. Anyways, I will mark your answer as accepted because I took your answer, added a ViewBag that will enable or disable the textbox based on any server errors and the method that sets the viewBag will only execute on the "form load" or the GET. – Pittfall Jun 13 '12 at 14:13
0

You can find out if the form is valid with jquery:

if($('form').valid()){

}
karaxuna
  • 26,752
  • 13
  • 82
  • 117