0

I know I'm doing something wrong, but not sure what I miss.

I have a form

        @using (Html.BeginForm("AddPayment", "Collecting", new { @invoiceId = invoice.Id }, FormMethod.Get, new { @class = "modal-link", @data_target = "#completePaymentModal" }))
        {
          <button id="completeInvoicePaymentButton" class="btn btn-primary">
            <i class="fa fa-money"></i>                 
          </button>
        }    

My modal container looks like this

<div class="modal inmodal" id="completePaymentModal">
  <div class="modal-dialog">
    <div id="mainContent" class="modal-content col-sm-12">



    </div>
  </div>
</div>

All of this above is in my index.cshtml.

My partial view looks like this.

@model ViewModels.InvoiceModel
@using Infrastructure.Resources;

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
      <div class="modal-header">
        <h4 class="pull-left">@UILabels.Collecting</h4>
      </div>
      <div class="modal-body">

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.Id)

        <p>S-a incasat plata facturii @Model.Id / @Model.InvoiceDate emisa clientului @Model.Company.Name </p>

        <div class="form-group">
          <div class="col-md-12">
            @Html.LabelFor(model => model.PaymentDate, htmlAttributes: new { @class = "control-label col-md-12 collecting-modal-label" })
            @Html.EditorFor(model => model.PaymentDate, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.PaymentDate, "", new { @class = "text-danger" })
          </div>
        </div>
        <div class="form-group">
          <div class="col-md-12">
            @Html.LabelFor(model => model.Observations, htmlAttributes: new { @class = "control-label col-md-12 collecting-modal-label" })
            @Html.TextAreaFor(model => model.Observations, htmlAttributes : new { @class = "form-control text-area"  } )
            @Html.ValidationMessageFor(model => model.Observations, "", new { @class = "text-danger" })
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <div class="form-group">
          <div class="col-lg-6">
            <button type="button" class="btn btn-white pull-left" data-dismiss="modal">
              @UILabels.Cancel
            </button>
          </div>
          <div class="col-lg-6 pull-right">
            <input type="submit" value="@UILabels.SaveBtn" class="btn btn-primary" />
          </div>
        </div>
      </div>
    </div>
}

My controller looks like this

  [HttpGet]
        public ActionResult AddPayment(int invoiceId)
        {
            return PartialView("AddPayment", _iInvoiceService.GetById(invoiceId));
        }

        [HttpPost]
        public ActionResult AddPayment(InvoiceModel invoice)
        {
            if (ModelState.IsValid)
            {

            }

            return PartialView("AddPayment", invoice);
        }

And my property looks like this ( from InvoiceModel )

[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)]
[Required(ErrorMessage = "Price is required")]
[Display(Name = "Data incasarii")]
public DateTime? PaymentDate { get; set; }

And I use this jquery to load my data

$(function () {
  $('.modal-link').on("click", function (e) {
    $form = $(this);
    e.preventDefault();
    //replace the get with this.href to load the edit page
    $.get($form.attr('action'), function (data) {
      //replace the content returned
      $("#mainContent").html(data);
    });
    //show the modal
    $('#completePaymentModal').modal({
      keyboard: true,
    }, 'show');
    return false;
  })
});

Everything works fine, except for the validation, when I submit my form Model.IsValid is false, but I don't get any errors on my view. I know that I have to get the validation from the server back to the client

$form.removeData('validator');
$form.removeData('unobtrusiveValidation');
$form.each(function () { $.data($(this)[0], 'validator', false); }); 
$.validator.unobtrusive.parse(".modal-link");

Something like this, but I have no idea where should I add this, as the Post is not done using JQuery. Do I need to do the POST from JQuery?

EDIT :

[HttpPost]
    public ActionResult AddPayment(InvoiceModel invoice)
{
    if (ModelState.IsValid)
    {

    }

    return RedirectToAction("Index");
}

$.get($form.attr('action'), function (data) {
  //replace the content returned
  $("#mainContent").html(data);
});
$form.data('validator', null);
$.validator.unobtrusive.parse($form);

Added this, still doesn't work.

CiucaS
  • 2,010
  • 5
  • 36
  • 63
  • You need to re-parse the validator after you have added the content - after `$("#mainContent").html(data);` - refer [Required field validations not working in JQuery Popup MVC 4](http://stackoverflow.com/questions/31768946/required-field-validations-not-working-in-jquery-popup-mvc-4/31769058#31769058) –  May 04 '17 at 09:59
  • @StephenMuecke check my edit. – CiucaS May 04 '17 at 10:11
  • No - in the success callback - ajax is async - your reparsing the validator before you ajax call has returned the view. –  May 04 '17 at 10:13
  • @StephenMuecke i've also tried to add the lines after `$("#mainContent").html(data);` same result. – CiucaS May 04 '17 at 10:21
  • What is `$form`? - that does not appear to be your `
    ` element
    –  May 04 '17 at 10:26
  • It is my form, else `$form.attr('action')` would not work. – CiucaS May 04 '17 at 10:29
  • But that is the form in your main view, not the form your dynamically loading using the `$.get()` method. –  May 04 '17 at 10:31
  • @StephenMuecke now I get what you saying, you are talking about my modal post form. But that is what I was asking, the post call isn't done using Ajax it's a MVC Form Post. – CiucaS May 04 '17 at 10:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/143390/discussion-between-stephen-muecke-and-ciucas). –  May 04 '17 at 10:35

0 Answers0