Return in a view-specific div

Asked

Viewed 220 times

1

I have a view where the contact form is at the bottom of the page, with validation via Dataannotations. When I click the send button, the controller checks the Modelstate, if it is not valid, it returns to the view with the validation messages.

However, it returns at the beginning of the page(view) and as the contact form is at the end of the page, it would have to scroll to the contact to see the messages.

In this case, I am using the bootstrap scrollspy and would like to know if there is how, at the time of returning the view, to return exactly in the contact div, to return directly in the error messages.

  • I’ve never tried this scrollpsy, but if in the form action you add something like action="~/Gravar#formId" doesn’t work?

  • 1

    Jamestk, in this case, as I am using mvc I don’t need to define the form action, but still wouldn’t give result, because the Controller returns the page view and I can’t pass the id of the div as parameter in this Return view();

3 answers

0

Well, I was able to do it this way:

I passed a value through Viewbag as the Gypsy Morrison Mendez spoke, and in Javascript I used the method that Tiago Falcão mentioned. Now when the page loads I check if it has the value in viewbag (which receives only qnd has validation) and only then executes the js function

0


Take the id of div that the fields have been validated and put in this command:

$('html, body').animate({ scrollTop: $('#IdDiv').offset().top }, 'slow');

0

If I understand how Scrollspy works, it rolls alone for a certain <div> by clicking a button when the document is ready ($(document).ready()). The validation information is generated by a Helper in Razor (@Html.ValidationSummary()). So, to get what you want, the following code must solve:

<div class="div-do-scrollspy">
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    ...
</div>
  • Gypsy, the validation I was able to do, the problem is that when the validation is done, the Controller returns the view but at the top, not in the contact div at the bottom of the page, but if I scroll the validations are there and I can’t pass the id of the div as parameter in the view(); of the Controller

  • @Fernandobigal You can go through ViewBag.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.