2

I have a simple view that is used to edit some basic information. Their is an "NA" checkbox if the data is not required.

   @Html.TextBoxFor(m => m.PersonInfo[i].Reference, "Enter a reference", new { disabled = Model.PersonInfo[i].NotApplicable }) 

   @Html.CheckBoxFor(m => m.PersonInfo[i].NotApplicable, new
       {
           onclick = "ChangeEnabledState(" + i + ");"

       })

When the user clicks the NA checkbox, it calles a javascript function that disables all the controls for that person.

I have a problem with the following scenario:

  1. User creates a new "Person info" and sets it to NA (so all fields get disabled)
  2. User then goes back into the edit view, unchecks the checkbox (enables the views)
  3. User enters some invalid data in one of the texboxes
  4. User clicks save, validation fails and the errors are added to the model
  5. As the textboxes being disabled is set based on the model, they are disabled but the checkbox inst checked. So the user has to click the checkbox twice to re enable them.

Is there a clean way of updateing the viewmodel so the fields are not disabled in the above scenario. I already tried a jquery document.ready to check the checbox and enable/disable but feel there must be a much more elegant solution.

4

1 回答 1

0

如果您使用 Ajax 表单,您有一个名为 AjaxOptions 的参数。对于 AjaxOptions,您可以定义 OnBegin、OnSucces、OnComplete、OnSucces、OnFailure。

在这里,您可以传入一个 javascript 函数,该函数在请求开始或完成或验证失败时调用,就像您的问题一样。

@using (Ajax.BeginForm("MethodName", "ControllerName", new AjaxOptions { OnFailure="EnableForm()" })
于 2012-11-19T08:22:13.983 回答