Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm new to MVC, please help on the below issue.

I have created Partial View which has few required field controls.

On the Index page, I'm showing employee list and also rendering the Partial View inside div control.

But when I say submit on the Partial View without entering any value, obviously
C#
ModelState.IsValid

will be false due to required field, but I couldn't able to show the same Index page with validation error message. Instead of that, It's opening a new tab and showing partial view page along with validation error message.

How to show the same index page along with validation error messages without using new tab.

Employee Class:

C#
public int EmployeeID { get; set; }
       [Display(Name="Name")]
       [Required(ErrorMessage="Please enter employee name.")]
       public string EmployeeName { get; set; }
       [Range(20, 60)]
       public int Age { get; set; }
       public int Gender { get; set; }


Partial View:
C#
@using (Html.BeginForm("Save", "Employee", FormMethod.Post, new { id = "displayer" }))
{
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.EmployeeID)

    <div class="form-group">
        @Html.LabelFor(model => model.EmployeeName, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.EmployeeName)
            @Html.ValidationMessageFor(model => model.EmployeeName)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Age, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Age)
            @Html.ValidationMessageFor(model => model.Age)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Gender, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Gender)
            @Html.ValidationMessageFor(model => model.Gender)
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
}



Controller Action:

C#
[HttpPost]
        public PartialViewResult Save(Employee employee)
        {
            if (ModelState.IsValid)
            {
                return PartialView("Index", _employees);
            }
            ModelState.AddModelError("AddEmployee", "Please enter all mandatory fields.");
            return PartialView("_onPageEdit", new Employee());
        }



Index Page:

<a id="loadview">Load</a>


HTML
<div id="displayer" style="height:100px; width:400px;">
</div>



JavaScript
$('#loadview').click(function () {
       $.ajax({
           type: 'POST',
           url: '@Url.Content("~/Employee/Loads")',
           success: function (data) {
               $('#displayer').html(data)
           },
           error: function (data) {
               alert('Failed');
           }
       });
   })


What I have tried:

I have tried to load div by using Ajax not able to achieve the same after validation.

JavaScript
$('#loadview').click(function () {
       $.ajax({
           type: 'POST',
           url: '@Url.Content("~/Employee/Loads")',
           success: function (data) {
               $('#displayer').html(data)
           },
           error: function (data) {
               alert('Failed');
           }
       });
   })
Posted
Updated 4-Oct-16 0:14am
v2
Comments
F-ES Sitecore 4-Oct-16 6:23am    
It's hard to know what is going on from the snippets you've shown, such as why it is opening in a new tab or how "Save" is instigated. However if you look at your save action you return the whole view when the validation works, but only the partial view when it doesn't. You need to return the "Index" view for both conditions if Save is the result of a form submission, and if Save is the result of an ajax call then how you fix it depends on things you haven't shown.

1 solution

use

HTML
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>


this javascript on layout page ..
it will validate.
can you just see this link.

http://www.codeproject.com/Answers/1135359/Display-model-error-on-same-popup-dialog-in-MVC#answer1[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900