Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am developing application which has Index.cshtml (main pg with searching criteria & Search button), _PartialView.cshtml (partial view displaying records with update/delete links), Edit.cshtml (View for updating).

After clicking Update link, popup modal dialog is opened containing Edit view (Edit.cshtml) I applied Required & StringLength attribute to some fields. If some of attributes not fulfilled, i want to display them in same popup. But in controller action method, I am returning View so modal popup disappears & Edit view is opened in div.

My records are displayed as grid in partialView (_PartialView.cshtml) with Update link and javascript function as

HTML
<a href="javascript:void(0);" class="anchorDetail" onclick="editFunc(@item.SrNo)" ">Update</a>



JavaScript
function editFunc(id) {
    var TeamDetailPostBackURL = '/Home/Edit';
    var $buttonClicked = $(this);
    var options = { "backdrop": "static", keyboard: true };

    $.ajax({
        type: "GET",
        url: TeamDetailPostBackURL,
        cache: false,
        contentType: "application/json; charset=utf-8",
        data: { "SrNo": id },
        datatype: "json",
        success: function (data) {
            $('#myModalContent').html(data);
            $('#myModal').css('display', 'block');
            $("#myModal").dialog({
                title: 'Edit Data',
                modal: true,
                bgiframe: true,
                show: 'slide',
                hide: 'slide',
                width: 750
            });
            $('#myModal').dialog("show");
        },

        error: function () {
            alert("Dynamic content load failed.");
        }
    });
}


My Edit.cshtml :

@using (Ajax.BeginForm("Edit", "Home", new AjaxOptions { UpdateTargetId = "divPartial", HttpMethod = "POST", InsertionMode = InsertionMode.Replace, OnBegin = "CallEditBegin", OnSuccess = "CallUnblock" }))
 {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)   
    
    <fieldset>
        
        <div class="editor-label" style="display:none" >
            @Html.LabelFor(model => model.SrNo)
        </div>
        <div class="editor-field" style="display:none">
            @Html.EditorFor(model => model.SrNo)
            @Html.ValidationMessageFor(model => model.SrNo)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.UserName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.UserName)
            @Html.ValidationMessageFor(model => model.UserName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ReaderName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ReaderName)
            @Html.ValidationMessageFor(model => model.ReaderName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.UploadDate)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.UploadDate, new { @readonly = "readonly" })
            @Html.ValidationMessageFor(model => model.UploadDate)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
 }



My HomeController.cs

C#
[HttpPost]
  public ActionResult Edit(UploadData uploadData)
  {
      if (!ModelState.IsValid)
      {

          return View(uploadData);
      }
       else 
      {
          // Processing Logic here

           string strResult = uploadDataBusinessLayer.InsertUpdateUploadData(uploadData, "Update");
            if (strResult == "Success")
            {   // Do return Partial View
            }
            else
            {   // How to display exception error in same popup ???
            }
      }
   }


Now some filed are required and ReaderName has restiction of min length of 6 characters. If any of the condition not fulfilled
C#
if (!ModelState.IsValid)
{
 
          return View(uploadData);
}
this block will be executed.

But what happened next, my popup dialog disappears & div will be open on main pg displaying error message.
I want to show them on popup itself in front of control.

I am confusing what to do in case of model errors.
Please give some advice, what I am missing.....

What I have tried:

Searched on google but cant find any suitable information.......
Posted
Updated 1-Jan-20 5:52am
v6
Comments
j snooze 29-Sep-16 17:29pm    
Your question could be improved by telling us what the error is and what you did to get the error(what was clicked on, or posted back to, which method is the error coming from) You can get all that by debugging the code.
Umesh AP 30-Sep-16 2:55am    
@j snooze : please see improved question & give some advice.

you need to add this script on layout page

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>


place on parital view

<script type="text/javascript" language=javascript>
$.validator.unobtrusive.parse(document);
</script>

this will wo
 
Share this answer
 
Comments
Umesh AP 3-Oct-16 5:47am    
@Member 12159992 - Thanks for reply. using your suggestion, now model errors are displayed in same popup without hitting controllers Edit action.
But next concern is about displaying database related exceptions in same popup dialog.
Umesh AP 3-Oct-16 5:59am    
@Member 12159992 - Please see my updated Edit controller action method.
I am concern about
// How to display exception error in same popup ???
this section......
Shravansingh Rajpurohit 1-Jan-20 12:06pm    
check my answer below
put any div on myedit.cshtml.


put this div in your myedit.cshtml.




on success or failure of that action call.
display your exception on this div.

using javascrript
 
Share this answer
 
Comments
Umesh AP 6-Oct-16 5:02am    
Thanks Raunak, for your helpful suggestion.
ajax - MVC Return Partial View as JSON - Stack Overflow[^]

read this one it's also resolved my problem.
 
Share this answer
 
Comments
Shravansingh Rajpurohit 1-Jan-20 12:05pm    
let me know if anybody have better solution for displaying serverside error on same partial view.

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