views:

19

answers:

1

I have a partial view that is being displayed as dialog. I need to do validation in my controller and return the error message in the dialog. How can I do that?

part of my code in the controller is as follows:

if (!String.Equals(newPassword, confirmPassword, StringComparison.Ordinal))
        {
            ModelState.AddModelError("Confirm Password", "The new password and confirmation password do not match.");
            return PartialView("PasswordDetails");
        }

the partial view is as follows:

<% using (Html.BeginForm("PasswordDetails", "User"))

{ %>

           <td>New password</td><td><%=Html.Password("newPassword")%><input type="hidden" id="ID" name="ID" /></td>
           </tr>
           <tr>
           <td>Confirm new password</td> <td><%=Html.Password("confirmPassword")%><%= Html.ValidationMessage("errors")%>
           </td>
           </tr>
           </table>
         <div class="rightalign" >
              <input type="submit" value="Accept" /> <input type="button"  value="Cancel" id="CloseDialog"/>
         </div>

<% } %>

A: 

I think your best bet is to use an inline frame here. Otherwise the POST will submit from the parent frame.

You could also use jQuery to form the request, send it to the server and then update based on the response. Something along the lines of...

$('input.submit').click(function(event){
    event.preventDefault();

    // form request
    $.ajax({ /* ... */ });
});
Krisc
I need to send the error message to the modal. How can I do that?
If you are using an inline frame, the error message response should fill within the inline frame. So just make sure the iframe is in the dialog.If you are using the AJAX call, you should be able to simply update the dialog div with jQuery: $('#mydiv').html(errorMessage)
Krisc