Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- In MVC, how to determine if partial view response was valid (on the client side)?
- [SomeController.cs]
- public class SomeController : Controller
- {
- public ActionResult ShowForm()
- {
- return View();
- }
- public ActionResult ValidateForm(MyFormModel FormModel)
- {
- FormValidationResults result = new FormValidationResults();
- result.IsValid = ModelState.IsValid;
- if (result.IsValid)
- {
- result.RedirectToUrl = "/Controller/Action";
- }
- this.Json(result);
- }
- }
- [FormValidationResult.cs]
- public class FormValidationResults
- {
- public bool IsValid { get; set; }
- public string RedirectToUrl { get; set; }
- }
- [View.js]
- $(document).ready(function()
- {
- $("#button").click(function()
- {
- var form = $("#myForm");
- $.ajax(
- {
- url: '/Some/ValidateForm',
- type: 'POST',
- data: form.serialize(),
- success: function(jsonResult)
- {
- if (jsonResult.IsValid)
- {
- window.location = jsonResult.RedirectToUrl;
- }
- }
- });
- });
- });
- @if (String.IsNullOrWhiteSpace(Layout))
- {
- // Do somthing if it is partial view
- }
- else
- {
- // Do somthing if it is full page view
- }
Add Comment
Please, Sign In to add comment